Jump to content

[SOLVED] Missing "1st Value" in Array


Recommended Posts

This piece of code loops through a table that I have and returns all the info stored therin,

 

EXCEPT for the first row in the table. From my basic grasp of arrays I know (read:think) that I need to subtract "1" from somewhere to get it to show all the results but I am battling.

 

Any help would be appretiated.

 

$query = "SELECT * FROM $DBtable";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());


while($row = mysql_fetch_array($result))

{
echo $row['ID'] . "---". $row['First'] . "---". $row['Sur'] . "---". $row['Com'];
echo "<p>";
}

Link to comment
Share on other sites

You need to remove this line:

 

$row = mysql_fetch_array($result) or die(mysql_error());

 

As the manual page says, the function returns the next row in the result set and advances the internal pointer. So before your loop runs, you're already pointing at the second row in the result set.

 

Edit: I shall move this topic to the FAQ/Code snippet repository once you've seen it and marked it solved. It's a fairly frequent question -- i guessed the question and answer from your title.

Link to comment
Share on other sites

Tx GingerRobot,

 

Removed that line and it worked like a charm:...

 

Bit frustrating though, I am teaching myself PHP and MySQL and have been using this site as a reference. 

 

http://www.tizag.com/mysqlTutorial/mysqlselect.php

 

This is not the first time that some of the code example has been "not 100% correct"....

 

Are there any other sites that you could point me to, and or "learning" suggestions?

 

Tx again,

 

Bryan

Link to comment
Share on other sites

I don't actually see the line i recommended you to take out in the code on that site.

 

However, i'd personally recommend the tutorials on the main site here: www.phpfreaks.com/tutorials. You'll find an excellent tutorial on basic database handling here.

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.