Welcome to Server Circle. It's a friendly site and all levels of experience are welcome. Be aware that we use cookies for your login.
Server Circle - Ask questions about Servers and get answers from experts.
Beta (0.30 sec)
REWARD: PHP and MYSQL while loop fails after first result

I'm really struggling with this PHP and MYSQL coding:

$result = mysql_query("SELECT ...") or die(mysql_error());

while ($array = mysql_fetch_array( $result )) {

$result = mysql_query("SELECT ... ) or die(mysql_error());

$detail = mysql_fetch_array($result);

$result = mysql_query("SELECT ...") or die(mysql_error());

$user = mysql_fetch_array($result);

print ("$user[2]");

}


The while loop only outputs one entry ... WHY ?!!!!

Can anyone please help ? I'll offer 50 of my points (and I think you get 50 points for a best answer too). Thanks.
Asked by:
crazee
128 points
 Report Abuse
 Share Page - Category: Programming - Tags: REWARD: PHP and MYSQL while loop fails after first result
 Enter your response
Please use Pastie.org to paste lengthy code or to fix formatting issues with code
  • Responses in reverse (2)

Best Answer

My suspicion is that you are re-using the same result variable, so your original query's results are overwritten with each subsequent result.

Try re-naming your variables like so:



$result = mysql_query("SELECT ...") or die(mysql_error());

while ($array = mysql_fetch_array( $result )) {

$detailresult = mysql_query("SELECT ... ) or die(mysql_error());

$detail = mysql_fetch_array($detailresult);

$userresult = mysql_query("SELECT ...") or die(mysql_error());

$user = mysql_fetch_array($userresult);

print ("$user[2]");

}


Response by:
jimrippon
873 points
Ah yes ! Thanks very much. I'll get the admins to give you the points.

Response by:
crazee
128 points


  • Related Questions
About Us : Contact Us : Etiquette : Terms : CDN Failover : ShorterURL : CDN Fallback : © 2013 Server Circle