This question already has an answer here:
I am developing a Chat Application. When I try to run the website, I get the error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in chat.php
PHP:
function get_msg()
{
$query = "SELECT 'sender','message' FROM 'chat'.'chat' ";
$run=mysql_query($query);
$messages = array();
while ($message = mysql_fetch_assoc($run))
{
$messages[]=array('sender'=>$message['Sender'],'message'=>$message['Message']);
}
return $messages;
}
</div>
Wrong quotes:
$query = "SELECT 'sender','message' FROM 'chat'.'chat' ";
^------^-^-------^------^----^-^----^--- *ALL* wrong
None of those need to be quoted.
Some things:
1.- Don't use mysql_ functions, are deprecated.
2.- When you get this error then your SQL has an error:
In this case the error is taht you are using wrong quotes, use ` (backticks):
function get_msg()
{
$query = "SELECT sender,message FROM chat.chat ";
$run=mysql_query($query);
$messages = array();
while ($message = mysql_fetch_assoc($run))
{
$messages[]=array('sender'=>$message['Sender'],'message'=>$message['Message']);
}
return $messages;
}
For the next time, use mysql_error() to debug your query
Might not be the answer to your question but just a bit of advice. THe mysql_
functions are beeing deprecated and will soon be whiped from existence. I advise you to learn PDO instead since is much more secure.
On http://nl3.php.net/pdo you'll find all you need to get it going.
Try that to help you debuging your request :
$run=mysql_query($query) or die(mysql_error());
And as said before, mysql_query will be soon deleting from php. Use PDO Class its smart :)