Jump to content


Photo

While loop not working...


  • Please log in to reply
9 replies to this topic

#1 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 23 July 2006 - 03:32 AM

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

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

Here is the while loop:
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

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

#2 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 04:29 AM

What does happen? just a blank page?
php version? 4 or 5?

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 04:40 AM

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

#4 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 23 July 2006 - 04:57 AM

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...?

#5 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 05:01 AM

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");

#6 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 23 July 2006 - 05:08 AM

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

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

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"

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 05:28 AM

The word time is a reserved word in MySQL, it's best to but backticks around the field names:
<?php $sql = "SELECT `messageID`, `subject`, `sender`, `time`, `read` FROM message WHERE receiver='$sid'"; ?>

Ken

#8 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 23 July 2006 - 03:34 PM

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:

$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



#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 03:46 PM

When strings are delimited by single quotes, variables are not expanded. Re-write your code as:
<?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
?>

Ken

#10 pocobueno1388

pocobueno1388
  • Members
  • PipPipPip
  • Advanced Member
  • 3,369 posts
  • LocationPalm Beach, Florida

Posted 23 July 2006 - 03:59 PM

It works! Thanks Ken, I appreciate all your help.

Thanks to everyone else who helped as well.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users