Jump to content

While loop not working...


pocobueno1388

Recommended Posts

For some reason I can't get my while loop to work correctly.

Here is where I pulled the information from the database:
[code]$sql = "SELECT messageID, subject, sender, time, read FROM message WHERE receiver='$sid'";
$result= mysql_query($sql);
[/code]

Here is the while loop:
[code]while ($row = mysql_fetch_assoc($result)){

echo '<td align="center"><input type="checkbox" name="delete"></td>';
echo '<td align="center"><a href=\"sendmessage.php?viewid=" . $row[messageID] . "\">" . $row[subject] . "</a></td>';
echo '<td align="center">$row[sender]</td>';
echo '<td align="center">$row[time]</td>';
echo '<td align="center"><font color="red">$row[read]</font></td></tr>';

}//end while[/code]

There is information entered into the database, so that stuff should be showing up...Any suggestions?

Any help would be greatly appreciated. Thank you XD
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/
Share on other sites

Have you put debugging statements in to display what you're retrieving? Also, put an "or die" clause on the mysql_query() statement. If there is an error there, this will catch it.. The indices to the $row array should be enclosed in quotes.

Ken
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62308
Share on other sites

Everything shows up okay except for the information in the while loop. It is not showing any of the information from the database. I will try to enclose everything in quotes to see if that works. I am using PHP 4 I believe.

I put the 'or die' into the query and got this error:

"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'read FROM message WHERE receiver='1'' at line 1"

Although, I do not see how to fix this error...?
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62316
Share on other sites

try runnign everything outside of the loop...

echo '<td align="center"><input type="checkbox" name="delete"></td>';
echo '<td align="center"><a href=\"sendmessage.php?viewid=" . $row[messageID] . "\">" . $row[subject] . "</a></td>';
echo '<td align="center">$row[sender]</td>';
echo '<td align="center">$row[time]</td>';
echo '<td align="center"><font color="red">$row[read]</font></td></tr>';

and that runs correctly? if OUTSIDE of the loop?

and i believe its mysql_query($sql) or die("i have died at the query");
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62317
Share on other sites

Nope...it is not even working outside of the loop. I think I just need to fix this:

[code]$sql = "SELECT messageID, subject, sender, time, read FROM message WHERE receiver='$sid'";[/code]

and everything will be okay. The problem is, I don't know what is wrong with that.

Again, here is the error I got:

"You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'read FROM message WHERE receiver='1'' at line 1"
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62319
Share on other sites

Ken - You fixed the $sql line XD but now there is another problem.

This script is for an inbox where users can send messages back and forth. What I have been trying to do the whole time is for the inbox information to show up. I have an entire table that says subject, from, time, read...and I wanted the information from the database to fill the table with that information for each message they had.

It seems to be understanding how many messages the person has...the only problem is it's only filling the information with

'$row[whatever]'....instead of displaying the actaul information.

Here is the part of the script that won'r work:

[code]$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read` FROM message WHERE receiver='$sid'";
$result= mysql_query($sql);

while ($row = mysql_fetch_assoc($result)){

echo '<td align="center"><input type="checkbox" name="delete"></td>';
echo '<td align="center"><a href=\"sendmessage.php?viewid=" . $row[messageID] . "\">" .

$row[subject] . "</a></td>';
echo '<td align="center">$row[sender]</td>';
echo '<td align="center">$row[time]</td>';
echo '<td align="center"><font color="red">$row[read]</font></td></tr>';

}//end while[/code]

Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62399
Share on other sites

When strings are delimited by single quotes, variables are not expanded. Re-write your code as:
[code]<?php
$sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read` FROM message WHERE receiver='$sid'";
$result= mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
        echo '<td align="center"><input type="checkbox" name="delete"></td>';
        echo '<td align="center"><a href="sendmessage.php?viewid="' . $row['messageID'] . '">' .  $row['subject'] . '</a></td>';
        echo '<td align="center">' . $row['sender'] . '</td>';
        echo '<td align="center">' . $row['time'] . '</td>';
        echo '<td align="center"><font color="red">' . $row['read'] . '</font></td></tr>';
}//end while
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/15370-while-loop-not-working/#findComment-62410
Share on other sites

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.