-2

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;
}
Anonymous
  • 11,347
  • 6
  • 32
  • 56
  • Your query returned fals as SQL had an error. Check mysql_error(). – ToBe Mar 18 '14 at 16:13
  • Are you connecting to the database first? – i-CONICA Mar 18 '14 at 16:13
  • Your error is because of single quotes on your db.table part. But your select is probably also not what you were trying to do. Check SQL syntax for SELECTS again. – ToBe Mar 18 '14 at 16:14
  • Data base are connected, i need to store the sender name and message in my sql after its need to display in the beowser. – user3434004 Mar 18 '14 at 16:14

4 Answers4

0

Wrong quotes:

$query = "SELECT 'sender','message' FROM 'chat'.'chat' ";
                 ^------^-^-------^------^----^-^----^--- *ALL* wrong

None of those need to be quoted.

Marc B
  • 348,685
  • 41
  • 398
  • 480
  • But PLEASE for the love of god, tell him to debug via mysql_error(). ;) – ToBe Mar 18 '14 at 16:16
  • And use a spell checker. I think the quote confusion is back ticks are useful for table names, not single quotes, to avoid errors with table names that could be similar to reserved words, etc. – i-CONICA Mar 18 '14 at 16:18
0

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

Sal00m
  • 2,902
  • 3
  • 20
  • 32
0

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.

vascofmdc
  • 13
  • 1
  • 7
0

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 :)

Neovea
  • 486
  • 3
  • 16