954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL While Loop Won't Show First Row

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?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP: Alternate Table Row Colours</title>
</head>

<body>
<?
  $connection = mysql_connect("mysql.thepianoman.info", "****", "*****");
  $database_select = mysql_select_db("mytests", $connection);
  $result = mysql_query("SELECT * FROM members", $connection);
  $row = mysql_fetch_array($result);

  while ($row = mysql_fetch_array($result)) {
      echo $row["id"];
      echo $row["firstname"];
      echo $row["lastname"];
      echo $row["username"];
      echo $row["password"];
      echo $row["email"];
  }
?>
</body>
</html>
<?
  mysql_close($connection);
?>


Thanks,

Anthony

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

just delete out the

"$row = mysql_fetch_array($result);"

and it would be fine.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

the one that is under the query.

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 

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

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

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!

ryan_vietnow
Posting Pro
578 posts since Aug 2007
Reputation Points: 28
Solved Threads: 71
 
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

antwan1986
Junior Poster
110 posts since May 2008
Reputation Points: 14
Solved Threads: 8
 

Well for me locally this works but online first loop is still not working. its still the same :S what can be the problem?

tnk1903
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

thanks Ryan Vietnow..it works..

jopianokid
Newbie Poster
1 post since May 2012
Reputation Points: 0
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You