Strange result using current and next

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Taxon
Forum Newbie
Posts: 7
Joined: Thu Jun 12, 2008 9:58 pm

Strange result using current and next

Post 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
dml
Forum Contributor
Posts: 133
Joined: Sat Jan 26, 2008 2:20 pm

Re: Strange result using current and next

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Strange result using current and next

Post by Apollo »

Use mysql_fetch_row instead of mysql_fetch_array.
Taxon
Forum Newbie
Posts: 7
Joined: Thu Jun 12, 2008 9:58 pm

Re: Strange result using current and next

Post by Taxon »

dml & Apollo-

Thanks. Now I understand.

Roger
Post Reply