Jump to content

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


Dan06

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.