Jump to content

Retrieving multiple rows with duplicate data


perky416

Recommended Posts

Hi Guys,

 

Im writing a message script for my website where users can send each other messages. When it comes to pulling the messages from the database and displaying them in the inbox im having some trouble.

 

When i use the code below i get the following error messages appear as soon as the page loads:

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/luser/public_html/website.co.uk/messages.php on line 16

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/website.co.uk/messages.php on line 21

 

Im assuming that the error messages are appearing as there are multiple rows using the same $username.

 

When i change

WHERE to='$username'

to

WHERE id='1'

it works perfectly.

 

$messages_query = mysql_query("SELECT * FROM messages WHERE to='$username'");
$messages_row = mysql_fetch_assoc($messages_query);

$from = $messages_row['from'];
$subject = $messages_row['subject'];

if (mysql_num_rows($messages_query) > 0)
{
while ($messages_row = mysql_fetch_array($messages_query));
{
	echo $from, $subject;
}
}

 

Does anybody have any ideas as to what im doing wrong?

 

Thanks

Link to comment
Share on other sites

Thank you Maq,

 

I didnt know about reserved words.

 

I changed 'to' to 'recipient' and 'from' to 'sender' and it works perfectly.

 

Thanks to your advice i now know to look up reserved words if i have a similar problem in the future.

Link to comment
Share on other sites

These types of errors usually refer to your query being invalid.  You can also get a specific error message by temporarily changing this line to:

$messages_query = mysql_query("SELECT * FROM messages WHERE to='$username'") or die(mysql_error());

 

You should be handling errors and exceptions properly though.  Have a read through this blog:

http://www.phpfreaks.com/blog/or-die-must-die

Link to comment
Share on other sites

Hi Maq,

 

I have just encountered another problem.

 

Using the following code im only getting data from the first row WHERE recipient='$username'. Im trying to get it to display data from all rows WHERE recipient='$username'  Is it a mysql problem or a php problem?

 

$messages_query = mysql_query("SELECT * FROM messages WHERE recipient='$username'");
$messages_row = mysql_fetch_array($messages_query);

$from = $messages_row['sender'];
$subject = $messages_row['subject'];

if (mysql_num_rows($messages_query) > 0)
{
while ($messages_row = mysql_fetch_array($messages_query));
{
	echo $from, $subject;
}
}

 

Thanks

Link to comment
Share on other sites

Because you are assigning $from and $subject before the loop so it's always the first row.  I fixed your code up a bit:

$sql = "SELECT * FROM messages WHERE recipient='$username'";

if (!$messages_query = mysql_query($sql)) 
{
   throw new Exception('SQL Error: ' . mysql_error());
}

if (mysql_num_rows($messages_query) > 0)
{
   while ($messages_row = mysql_fetch_array($messages_query))
   {
      echo "From: " . $messages_row['sender'];
      echo "Subject: " . $messages_row['subject'];
   }
}
else
{
   echo "No results.";
}

Link to comment
Share on other sites

Thanks mate I appreciate it.

 

Iv noticed that without the code below, the details from the first row are not displayed. Would you happen to know why this is? Im still learning and would like to understand it better.

 

$sql = "SELECT * FROM messages WHERE recipient='$username'";

if (!$messages_query = mysql_query($sql)) 
{
   throw new Exception('SQL Error: ' . mysql_error());
}

 

Thanks

Link to comment
Share on other sites

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.