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
Strange result using current and next
Moderator: General Moderators
Re: Strange result using current and next
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
Use mysql_fetch_row instead of mysql_fetch_array.
Re: Strange result using current and next
dml & Apollo-
Thanks. Now I understand.
Roger
Thanks. Now I understand.
Roger