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