Jump to content

mysql_fetch_array() being a pain


satal keto

Recommended Posts

I am trying to create a simple messaging system for my website and I am having some problems with mysql_fetch_array().

the code I am using is;

$query = "SELECT * FROM messages WHERE ToPlayer='" . $UserID . "'";
$result = mysql_query($query) or die(mysql_error());
$subject = "Inbox";
if (mysql_num_rows($result) > 0)
{
	// If the user has messages
	$content = "<br /><table width='95%' align='center' border='1px'><tr><td>Subject</td><td>From</td><td>Date/Time</td></tr>";
	while ($i = mysql_fetch_array($result, MYSQL_ASSOC)) 
	{
		// For each result
		$MsgID = $i['MsgID'];
		$MsgSubject = $i['Subject'];
		$MsgMessage = $i['Message'];
		$MsgFrom = $i['FromPlayer'];
		$MsgDate = $i['DateSent'];
		$query = "SELECT Username FROM users WHERE UserID='" . $MsgFrom . "'";
		$result = mysql_query($query) or die(mysql_error());
		$result = mysql_fetch_array($result);
		$MsgFrom = $result['Username'];
		if ($i['Read'] == 1)
		{
			$MsgRead = true;
		}
		else
		{
			$MsgRead = false;
		}
		$content = $content . "<tr><td>";
		if ($MsgRead == false)
		{
			$content = $content . "<strong>";
		}
		$content = $content . "<a href='Msgs.php?view=Msg&MsgID=" . $MsgID . "'>" . $MsgSubject . "</a>";
		if ($MsgRead == false)
		{
			$content = $content . "</strong>";
		}
		$content = $content . "</td><td>" . $MsgFrom . "</td><td>" . $MsgDate . "</td></tr>";

	}
	$content = $content . "</table><br />";
}

The error message I am recieving is

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in L:\WOS\www\tian\Msgs.php on line 37

The code on line 37 being

while ($i = mysql_fetch_array($result, MYSQL_ASSOC))

Now I have used exactly the same code in other area's of my website for the same sort of purpose but I haven't had this problem.

I can't see anything wrong with that code at all but I am no expert at PHP

I was wondering whether anyone else could see anything wrong with that code.

 

Thanks for any help in advance

Satal Keto  :D

Link to comment
Share on other sites

		// For each result
		$MsgID = $i['MsgID'];
		$MsgSubject = $i['Subject'];
		$MsgMessage = $i['Message'];
		$MsgFrom = $i['FromPlayer'];
		$MsgDate = $i['DateSent'];
		$query = "SELECT Username FROM users WHERE UserID='" . $MsgFrom . "'";
		$result = mysql_query($query) or die(mysql_error());
		$result = mysql_fetch_array($result);

 

It is never good to use the same names twice, especially when you are in the same loop that depends on $result. Rename that far to $result2 or something like that. It should fix the problem.

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.