MySQL While Loop Won't Show First Row

Thread Solved

Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

MySQL While Loop Won't Show First Row

 
0
  #1
May 29th, 2008
Hi everyone and thanks for reading. This problems been driving me nuts for the last day and I'm completely stuck. I made a dummy table of users to test out SQL connections but everytime I echo out the information, it keeps dropping the first record, and jumps straight to record two onwards. If I sort the query using ORDER BY 'id' desc, then it will drop the last record and start on the one before the last; any ideas whats gone wrong?

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>PHP: Alternate Table Row Colours</title>
  6. </head>
  7.  
  8. <body>
  9. <?
  10. $connection = mysql_connect("mysql.thepianoman.info", "****", "*****");
  11. $database_select = mysql_select_db("mytests", $connection);
  12. $result = mysql_query("SELECT * FROM members", $connection);
  13. $row = mysql_fetch_array($result);
  14.  
  15. while ($row = mysql_fetch_array($result)) {
  16. echo $row["id"];
  17. echo $row["firstname"];
  18. echo $row["lastname"];
  19. echo $row["username"];
  20. echo $row["password"];
  21. echo $row["email"];
  22. }
  23. ?>
  24. </body>
  25. </html>
  26. <?
  27. mysql_close($connection);
  28. ?>

Thanks,

Anthony
Last edited by antwan1986; May 29th, 2008 at 8:38 pm.
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: MySQL While Loop Won't Show First Row

 
1
  #2
May 29th, 2008
just delete out the

"$row = mysql_fetch_array($result);"

and it would be fine.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: MySQL While Loop Won't Show First Row

 
0
  #3
May 29th, 2008
the one that is under the query.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: MySQL While Loop Won't Show First Row

 
0
  #4
May 29th, 2008
Originally Posted by ryan_vietnow View Post
just delete out the

"$row = mysql_fetch_array($result);"

and it would be fine.
Your 100% right, that worked. I'm totally confused now because I'm almost positive that I've had queries work properly with that command before. Can you elaborate on why this happened and why taking that line out fixed it?

Thank you very much,


Anthony
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 569
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is offline Offline
Posting Pro

Re: MySQL While Loop Won't Show First Row

 
0
  #5
May 29th, 2008
it is because that $row is supposed to be 0 in the opening of the while loop which will get row 0 or the first row in the result.If you set the $row into the mysql_fetch_array,the result will not be zero instead its 1 so you always get row 1 or the second row.Happy coding friend!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 110
Reputation: antwan1986 is an unknown quantity at this point 
Solved Threads: 8
antwan1986's Avatar
antwan1986 antwan1986 is offline Offline
Junior Poster

Re: MySQL While Loop Won't Show First Row

 
0
  #6
May 29th, 2008
Originally Posted by ryan_vietnow View Post
it is because that $row is supposed to be 0 in the opening of the while loop which will get row 0 or the first row in the result.If you set the $row into the mysql_fetch_array,the result will not be zero instead its 1 so you always get row 1 or the second row.Happy coding friend!
I understand now; by using mysql_fetch_array it's bringing the pointer forward, so that the while loop is missing the first record (because I had two mysql_fetch_array's!)

Thank you for explaining that.

Anthony
"Beneath this mask there is more than flesh. Beneath this mask there is an idea, Mr. Creedy, and ideas are bulletproof." - V
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC