0

Im trying to do a mysql query but for some reason this is the error I'm getting:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home1/server/public/chat/includes/functions/chat.func.php on line 9

Line 9 is as follows:

l6 $query = "SELECT `Sender`, `Message` FROM `database_chat`.`chat` ORDER BY `Msg_ID` DESC";    
l7 $run = mysql_query($query);
l8 $messages = array();
l9 while($message = mysql_fetch_assoc($run)) {...

database's name is 'database_chat' and 'chat' is the table...

For some reason it ain't connecting, what am I missing?

I've double-checked that the table elements are correctly written....

user2766367
  • 399
  • 2
  • 4
  • 9
  • You probably have an error in your query – Ibu Nov 06 '13 at 17:03
  • That means the query failed. Try executing it manually (using phpMyAdmin or command line) and see what you get? – Amal Murali Nov 06 '13 at 17:04
  • 2
    mysql_* is deprecated, don't use it, but still if you want to solve your issue, try this: `$run = mysql_query($query) or die(mysql_error());` – Horse SMith Nov 06 '13 at 17:05
  • Please don't use `mysql_*` functions anymore, they are deprecated. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for details. Instead you should learn about [prepared statements](http://bobby-tables.com/php.html) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Marcel Korpel Nov 06 '13 at 17:06
  • @AmalMurali the query that I ran on phpMyAdmin is good, everything is ok...Its the first time I try to run php and mysql on a hosting server, so Im getting the hang of this.... – user2766367 Nov 06 '13 at 17:07
  • @user2766367 A good beginning would be to start [here](http://www.php.net/manual/en/book.mysqli.php) or [here](http://www.php.net/manual/en/book.pdo.php) instead. – Horse SMith Nov 06 '13 at 17:08

4 Answers4

1

Try to use error checking in your code:

$con = mysql_connect(...);

if (!$con){
   // Error handling here
   print_r("SQL Error:". mysql_error());
   exit;
}

$run = mysql_query($query);

if (!$run){
   // Error handling here
   print_r("SQL Error:". mysql_error());
   exit;
}

This will only help you find the real problem you are having, which i am assuming is an error in your query.

Important Note: mysql_* functions are deprecated, use mysqli_* or PDO

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
Ibu
  • 42,752
  • 13
  • 76
  • 103
0

Try this to see if there's a problem in your query itself:

$run = mysql_query($query) or die(mysql_error());

Note that you should only use or die(mysql_error()); for debugging - remove it for production use.

Also, assuming you've already connected to the database, have you selected the database before you run the query?

mysql_select_db('database_chat');
Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
0

mysql_query($query) requires a resource. An example is listed below using $link as the resource.

$link = mysql_connect($host, $user, $pass);
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db($db, $link);
    if (!$db_selected) {
        die('Can\'t use database : ' . mysql_error());
    }

$results = mysql_query($query, $link);
bassxzero
  • 4,838
  • 22
  • 34
  • `mysql_query` does not need a resource. It's an optional second parameter - if it's not given, it will use the most recently opened connection as a default. – andrewsi Nov 06 '13 at 17:27
0

in your 17th line query requires a database connection.. follow this example..

    $con=mysqli_connect("localhost","usernamename","password","db_name");
$sql="SELECT * from db_table'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{

     <---- your code here ----->

     }
Vivek S
  • 2,010
  • 1
  • 13
  • 15