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
https://forums.phpfreaks.com/topic/43353-mysql_fetch_array-being-a-pain/
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.

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.