Jump to content

unexpected error


squiblo

Recommended Posts

$query = mysql_query("SELECT * FROM chat_likes WHERE chat_name='{$chat}' AND user_id='{$user_id}'");
while($row = mysql_fetch_assoc($query)) // line 10
{
//  
}

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in ....... on line 10

 

I do not understand why I am getting this error, the table and field names are correct.

 

Link to comment
https://forums.phpfreaks.com/topic/212981-unexpected-error/
Share on other sites

Hi there,

 

Check (by echoing to screen) that the sql query has been populated correctly, an personally I would alter it a tad to be like this:-

echo $sqlQuery = "SELECT * FROM `chat_likes` WHERE `chat_name` = '".$chat."' AND `user_id` = '".$user_id."' ";
$query = mysql_query($sqlQuery) or die(mysql_error());

 

Just some suggestions there, but sometimes spaces make a difference..

 

Cheers,

Rw

Link to comment
https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109268
Share on other sites

Your query failed for whatever reason. You should always check the return value from mysql_query() and act appropriately. It will return false if the query failed, then look at mysql_error()

$query = mysql_query("SELECT * FROM chat_likes WHERE chat_name='{$chat}' AND user_id='{$user_id}'");
if (! $query) { // THE QUERY FAILED
    /* You should do something here to tell the user there is a problem and 
        maybe provide a link back to another page or something */
    trigger_error(mysql_error(), E_USER_ERROR);
}
while($row = mysql_fetch_assoc($query)) // line 10

 

I use trigger_error() rather than die() so I can leave the code in when I go to production. That way the error gets logged and the user doesn't get a blank screen.

Link to comment
https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109280
Share on other sites

This is the full php file, $user_id is from an included file

session_start();
include('../../../connect.php');
include('../../../global.php');
include('../../../functions.php');

$chat = mysql_real_escape_string($_POST['data']);

$query = mysql_query("SELECT * FROM chat_likes WHERE chat_name='{$chat}' AND user_id='{$user_id}'") or file_error($file_name, mysql_error());
while($row = mysql_fetch_assoc($query))
{
if(mysql_num_rows($query)==1)
{
	$user = $row['user_id'];
	$likes = $row['likes'];

	if(($user == $user_id)&&($likes == "0"))
	{
		$query = mysql_query("UPDATE chat_likes SET likes='1' WHERE chat_name='{$chat}' AND user_id='{$user_id}'") or file_error($file_name, mysql_error());
	}

}
else
{
	$query = mysql_query("INSERT INTO chat_likes VALUES('{$chat}', '{$user_id}', '1')") or file_error($file_name, mysql_error());
}
}

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109283
Share on other sites

Your while(){} loop works the first pass through it.

 

However, your code inside the while {} makes no sense. You are both checking mysql_num_rows() inside the loop (if you are inside the loop there WAS at least one row in the result set) AND you are reusing the $query variable, thereby wiping it out, so of course your mysql_fetch_assoc() statment is producing an error when it tries to fetch something from the $query result resource the second pass through the while loop.

Link to comment
https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109355
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.