Jump to content

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
https://forums.phpfreaks.com/topic/167483-solved-missing-1st-value-in-array/
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.

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

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.

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

 

:-[sorry GingerRobot, I have been battling with that one so so I had progressed to the next page: Correct (incorrect) link above...

 

Have headed over to the tutorials section, they look great - Thank you for the advice - Have a great Sunday..

 

×
×
  • 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.