943,669 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 7612
  • PHP RSS
May 29th, 2008
0

MySQL While Loop Won't Show First Row

Expand Post »
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?

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 14
Solved Threads: 8
Junior Poster
antwan1986 is offline Offline
110 posts
since May 2008
May 29th, 2008
2

Re: MySQL While Loop Won't Show First Row

just delete out the

"$row = mysql_fetch_array($result);"

and it would be fine.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
May 29th, 2008
0

Re: MySQL While Loop Won't Show First Row

the one that is under the query.
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
May 29th, 2008
0

Re: MySQL While Loop Won't Show First Row

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
Reputation Points: 14
Solved Threads: 8
Junior Poster
antwan1986 is offline Offline
110 posts
since May 2008
May 29th, 2008
0

Re: MySQL While Loop Won't Show First Row

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!
Reputation Points: 28
Solved Threads: 71
Posting Pro
ryan_vietnow is offline Offline
578 posts
since Aug 2007
May 29th, 2008
0

Re: MySQL While Loop Won't Show First Row

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
Reputation Points: 14
Solved Threads: 8
Junior Poster
antwan1986 is offline Offline
110 posts
since May 2008
Aug 23rd, 2011
0
Re: MySQL While Loop Won't Show First Row
Well for me locally this works but online first loop is still not working. its still the same what can be the problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tnk1903 is offline Offline
1 posts
since Jan 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: finding a primary key that is auto_incr
Next Thread in PHP Forum Timeline: Newsletter Script Please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC