squiblo Posted September 9, 2010 Share Posted September 9, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/ Share on other sites More sharing options...
rwwd Posted September 9, 2010 Share Posted September 9, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109268 Share on other sites More sharing options...
squiblo Posted September 9, 2010 Author Share Posted September 9, 2010 When I echo my results, the results on screen are what I expect Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109271 Share on other sites More sharing options...
DavidAM Posted September 9, 2010 Share Posted September 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109280 Share on other sites More sharing options...
squiblo Posted September 9, 2010 Author Share Posted September 9, 2010 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] Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109283 Share on other sites More sharing options...
chiprivers Posted September 9, 2010 Share Posted September 9, 2010 Have you double checked your table name and column names are correct? Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109347 Share on other sites More sharing options...
PFMaBiSmAd Posted September 9, 2010 Share Posted September 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212981-unexpected-error/#findComment-1109355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.