Page 1 of 1

Strange result using current and next

Posted: Sun Jul 13, 2008 2:07 pm
by Taxon
Please help me understand what I am doing wrong with use of current and next.

I created the following MySQL database for purpose of demonstration:

CREATE DATABASE `Test` ;

CREATE TABLE `Tble` (
`Field1` TEXT NOT NULL ,
`Field2` TEXT NOT NULL ,
`Field3` TEXT NOT NULL
) ENGINE = MYISAM ;

INSERT INTO `Test`.`Tble` (
`Field1` ,
`Field2` ,
`Field3`
)
VALUES (
'One', 'Two', 'Three'
);

Then I created the following module (TestOneTwoThree.php) to demonstrate the result I am experiencing:

<html>
<?php
error_reporting(E_ALL);

$host = "localhost";
$user = "Roger";
$password = "xxxxxx";
$connection = mysql_connect($host, $user, $password)
or die(mysql_error() . "<br>");

$dbname = "Test";
mysql_select_db($dbname)
or die(mysql_error());

$sql = "SELECT * FROM Tble" ;
$result = mysql_query($sql)
or die(mysql_error() . "<br>");
$row = mysql_fetch_array($result);
$cell = current($row);
echo $cell;
$cell = next($row);
echo $cell
$cell = next($row);
echo $cell;
$cell = next($row);
echo $cell
$cell = next($row);
echo $cell;
$cell = next($row);
echo $cell;
?>
</html>

And, this is the resulting display:

OneOneTwoTwoThreeThree

What am I doing wrong in my use of current and next?

Thanks,

Roger

Re: Strange result using current and next

Posted: Sun Jul 13, 2008 4:21 pm
by dml
By default, mysql_fetch_array returns an array with two entries for each column, one with the column name as the key, another with the column number as the key. So if you do var_dump($row), you'll see something like array(0=>"One", "Field1"=>"One",...). There's an optional extra argument to mysql_fetch_array if you want just the column numbers or just the column names.

Re: Strange result using current and next

Posted: Sun Jul 13, 2008 4:36 pm
by Apollo
Use mysql_fetch_row instead of mysql_fetch_array.

Re: Strange result using current and next

Posted: Mon Jul 14, 2008 12:52 am
by Taxon
dml & Apollo-

Thanks. Now I understand.

Roger