Author Topic: [SOLVED] Missing "1st Value" in Array  (Read 11548 times)

0 Members and 1 Guest are viewing this topic.

Offline NewbieBryanTopic starter

  • Irregular
  • Posts: 19
    • View Profile
[SOLVED] Missing "1st Value" in Array
« on: July 26, 2009, 05:56:08 AM »
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.

Code: [Select]
$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>";
}

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Posts: 4,133
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Missing "1st Value" in Array
« Reply #1 on: July 26, 2009, 05:57:59 AM »
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.
« Last Edit: July 26, 2009, 05:59:37 AM by GingerRobot »

Offline NewbieBryanTopic starter

  • Irregular
  • Posts: 19
    • View Profile
Re: Missing "1st Value" in Array
« Reply #2 on: July 26, 2009, 06:04:43 AM »
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

Offline GingerRobot

  • Guru
  • Fanatic
  • *
  • Posts: 4,133
  • Gender: Male
  • Call me Ben
    • View Profile
Re: Missing "1st Value" in Array
« Reply #3 on: July 26, 2009, 06:08:26 AM »
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.

Offline NewbieBryanTopic starter

  • Irregular
  • Posts: 19
    • View Profile
Re: [SOLVED] Missing "1st Value" in Array
« Reply #4 on: July 26, 2009, 06:14:42 AM »
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..