Jump to content

[SOLVED] do/while versus while loop confusion...


Recommended Posts

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>

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.