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

6
  • You probably have an error in your query Commented Nov 6, 2013 at 17:03
  • That means the query failed. Try executing it manually (using phpMyAdmin or command line) and see what you get? Commented Nov 6, 2013 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()); Commented Nov 6, 2013 at 17:05
  • Please don't use mysql_* functions anymore, they are deprecated. See Why shouldn't I use mysql_* functions in PHP? for details. Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is a good tutorial. Commented Nov 6, 2013 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.... Commented Nov 6, 2013 at 17:07

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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');

3 Comments

yup, I've selected the database and connected to it once I connect, I run the code I posted
Thank you very much for your answer, just showemd me what was wrong. Very helpful!
@user2766367 - if you've figured this out, you can add an answer with the solution in it, and accept it when you can.
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);

1 Comment

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

     }

1 Comment

Why that apostrophe after db_table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.