Dan06 Posted January 22, 2009 Share Posted January 22, 2009 In my php page, in the head section I perform a few mysql queries. I then use these queries to display page content in the body. I don't understand why, but when I use a while loop only the latest/last set of queried information is displayed. However, when I use a Do while loop all the queried information is displayed. Can someone help me understand what is going on? Here is my code: <html> <head> <?php include("objects.php"); $dropmenu = new dropmenuDisplay(); $format = new stringFormat(); $dbconn = new dbConnection(); session_start(); $messagesQuery = sprintf("SELECT msgSender, message_content.msgId, msgDate, msgContent FROM utility.message_inbox, utility.message_content WHERE msgRecipient = %s AND message_inbox.msgId = message_content.msgId", $format->formatValue($_SESSION["userId"])); $msgQueryResult = mysql_query($messagesQuery, $dbconn->conn()) or die("Inbox Message Query Error: " . $messagesQuery . " " . mysql_error()); $msgInfo = mysql_fetch_assoc($msgQueryResult); $membershipIdQuery = sprintf("SELECT membershipId FROM utility.user_information WHERE userId = %s", $format->formatValue($msgInfo["msgSender"])); $memIdQueryResult = mysql_query($membershipIdQuery, $dbconn->conn()) or die("Membership Id Query Error: " . $membershipIdQuery . " " . mysql_error()); $membershipIdValue = mysql_fetch_assoc($memIdQueryResult); $profileImgQuery = sprintf("SELECT profileImage FROM utility.profile WHERE membershipId = %s", $format->formatValue($membershipIdValue["membershipId"])); $profileImgResult = mysql_query($profileImgQuery, $dbconn->conn()) or die("Profile Image Query Error: " . $profileImgQuery . " " . mysql_error()); $profileImgLoc = mysql_fetch_assoc($profileImgResult); ?> </head> <body> <?php while ($msgInfo = mysql_fetch_assoc($msgQueryResult)){ echo '<div class="displayMsg">' . '<img src="' . $profileImgLoc["profileImage"] . '" width="100px" height="100px" />' . ' ' . $msgInfo["msgDate"] . ' ' . $msgInfo["msgContent"] . '</div>'; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141867-solved-dowhile-versus-while-loop-confusion/ Share on other sites More sharing options...
MadTechie Posted January 22, 2009 Share Posted January 22, 2009 if i am reading your code correctly you should only be missing the first record!! try the code below and read the comments (i have commented out 1 line) <html> <head> <?php include("objects.php"); $dropmenu = new dropmenuDisplay(); $format = new stringFormat(); $dbconn = new dbConnection(); session_start(); $messagesQuery = sprintf("SELECT msgSender, message_content.msgId, msgDate, msgContent FROM utility.message_inbox, utility.message_content WHERE msgRecipient = %s AND message_inbox.msgId = message_content.msgId", $format->formatValue($_SESSION["userId"])); $msgQueryResult = mysql_query($messagesQuery, $dbconn->conn()) or die("Inbox Message Query Error: " . $messagesQuery . " " . mysql_error()); #$msgInfo = mysql_fetch_assoc($msgQueryResult); //This Get the first record $membershipIdQuery = sprintf("SELECT membershipId FROM utility.user_information WHERE userId = %s", $format->formatValue($msgInfo["msgSender"])); $memIdQueryResult = mysql_query($membershipIdQuery, $dbconn->conn()) or die("Membership Id Query Error: " . $membershipIdQuery . " " . mysql_error()); $membershipIdValue = mysql_fetch_assoc($memIdQueryResult); $profileImgQuery = sprintf("SELECT profileImage FROM utility.profile WHERE membershipId = %s", $format->formatValue($membershipIdValue["membershipId"])); $profileImgResult = mysql_query($profileImgQuery, $dbconn->conn()) or die("Profile Image Query Error: " . $profileImgQuery . " " . mysql_error()); $profileImgLoc = mysql_fetch_assoc($profileImgResult); ?> </head> <body> <?php /* Now gets the second, third, fouth etc, with a DO the first record would be echo'ed then it would continue as normal! But by removing the line above (see #) the line below will get the first record */ while ($msgInfo = mysql_fetch_assoc($msgQueryResult)) { echo '<div class="displayMsg">' . '<img src="' . $profileImgLoc["profileImage"] . '" width="100px" height="100px" />' . ' ' . $msgInfo["msgDate"] . ' ' . $msgInfo["msgContent"] . '</div>'; } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/141867-solved-dowhile-versus-while-loop-confusion/#findComment-742818 Share on other sites More sharing options...
Dan06 Posted January 22, 2009 Author Share Posted January 22, 2009 Thanks for the clarification... I was confused because I incorrectly thought that each time the $msgInfo = mysql_fetch_assoc($msgQueryResult) code is called it automatically starts from the beginning, i.e. the first array set. Link to comment https://forums.phpfreaks.com/topic/141867-solved-dowhile-versus-while-loop-confusion/#findComment-743518 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.