8

Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given

I'm very confused with this error, it shows when I try to return a result from the DB that doesn't exist ... I tried mysql_num_rows() but it returns the same error but instead of mysql_fetch_assoc expects ... it says mysql_num_rows() expects ...

I set error_reporting(0) to avoid showing this error, but I'm not satisfied with this solution ...

Community
  • 1
  • 1
Naughty.Coder
  • 3,922
  • 7
  • 32
  • 41
  • 2
    ignoring the warning (error_reporting(0)) is not a solution at all. Post some actual code - you're doing something wrong, but it's not clear what. – timdev Jun 27 '10 at 23:58
  • Providing the code itself might be helpful. – Robus Jun 27 '10 at 23:58
  • Sounds like you have a mysql error. Check the output of mysql_error() – Chris Henry Jun 28 '10 at 00:09
  • Yeah , I had an error in my sql query, I checked with mysql_erro() – Naughty.Coder Jun 28 '10 at 00:11
  • Dup of [mysql_fetch_assoc error, can't seem to figure out what the problem is](http://stackoverflow.com/questions/2372029/) – outis Jan 14 '12 at 06:06
  • Make sure you are connected the mysql and your database at the top of error page.This was my problem. Hope fully this one helps. –  Apr 11 '12 at 08:19
  • I had finally solved same issue on my site :D. This only happens because when your script want find the table you had specified in your script. So make sure that the specified table is exist. – Ravi Bhalodiya Jan 09 '15 at 12:27

6 Answers6

13

Here's the proper way to do things:

<?PHP
$sql = 'some query...';
$result = mysql_query($q);

if (! $result){
   throw new My_Db_Exception('Database error: ' . mysql_error());
}

while($row = mysql_fetch_assoc($result)){
  //handle rows.
}

Note the check on (! $result) -- if your $result is a boolean, it's certainly false, and it means there was a database error, meaning your query was probably bad.

timdev
  • 61,857
  • 6
  • 82
  • 92
  • 1
    `die()` is the worst method of error handling. especially with exposing mysql error to the user. Use throw or return instead. – Your Common Sense Jun 28 '10 at 00:20
  • 1
    It beats no error handling. And this is example code. But editing just for you! – timdev Jun 28 '10 at 00:21
  • And obviously on a production site, whatever catches that exception should never display the message to the user. – timdev Jun 28 '10 at 00:22
  • This example is supposed to be copied and pasted by the OP intact. You are responsible for the consequences of examples you have posted on SO. – Your Common Sense Jun 28 '10 at 00:23
  • Not just for him - all of us :) and it got you another +1 - and col. shrapnel, do those consequences include death and dismemberment? – Dan Heberden Jun 28 '10 at 00:23
  • 4
    Any example code I write on SO is absolutely not meant to be blindly copied/pasted into an application. It's supposed to be read and comprehended. The resulting knowledge should then be applied to the asker's particular problem. That said, in the future, I won't use die (or even throw) ... I'll just leave a comment like `//handle the error` – timdev Jun 28 '10 at 00:27
  • 1
    @timdev be sure to make it `//handle the error or the col will get you` –  Jun 28 '10 at 00:38
  • As a matter of fact, internet is overfilled with bad practice PHP codes. Every time you don't repeat it, yo make the world better. And I don't understand how it can be supposed not to be blindly copied/pasted into an application. Everyone asking questions for that purpose only. lol Mr.X :) – Your Common Sense Jun 28 '10 at 00:39
  • @Col: Agreed 99% (I try to make my answers require some thought/learning to use) @Mr.X: I'm so going to do that from now on. – timdev Jun 28 '10 at 00:51
  • Class 'My_Db_Exception' not found.... I'm a beginner to intermediate php programmer, and I cant figure out what timdev intends to have here? Shouldnt it be, echo "Database: " . mysqlerror($q); ? – phpN00b Mar 23 '18 at 03:30
1

You must check if result returned by mysql_query is false.

$r = mysql_qyery("...");
if ($r) {
  mysql_fetch_assoc($r);
}

You can use @mysql_fetch_assoc($r) to avoid error displaying.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
1

This is how you should be using mysql_fetch_assoc():

$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {
    // Do stuff with $row
}

$result should be a resource. Even if the query returns no rows, $result is still a resource. The only time $result is a boolean value, is if there was an error when querying the database. In which case, you should find out what that error is by using mysql_error() and ensure that it can't happen. Then you don't have to hide from any errors.

You should always cover the base that errors may happen by doing:

if (!$result) {
    die(mysql_error());
}

At least then you'll be more likely to actually fix the error, rather than leave the users with a glaring ugly error in their face.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

The proper syntax is (in example):

$query = mysql_query('SELECT * FROM beer ORDER BY quality');
while($row = mysql_fetch_assoc($query)) $results[] = $row;
timdev
  • 61,857
  • 6
  • 82
  • 92
cypher
  • 6,822
  • 4
  • 31
  • 48
0

You don't need to prevent this error message!
Error messages are your friends!
Without error message you'd never know what is happened.
It's all right! Any working code supposed to throw out error messages.

Though error messages needs proper handling. Usually you don't have to to take any special actions to avoid such an error messages. Just leave your code intact. But if you don't want this error message to be shown to the user, just turn it off. Not error message itself but daislaying it to the user.

ini_set('display_errors',0);
ini_set('log_errors',1);

or even better at .htaccess/php.ini level
And user will never see any error messages. While you will be able still see it in the error log.
Please note that error_reporting should be at max in both cases.

To prevent this message you can check mysql_query result and run fetch_assoc only on success.
But usually nobody uses it as it may require too many nested if's.
But there can be solution too - exceptions!

But it is still not necessary. You can leave your code as is, because it is supposed to work without errors when done.

Using return is another method to avoid nested error messages. Here is a snippet from my database handling function:

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbget: ".mysql_error()." in ".$query);
    return false;
  }
  if (!mysql_num_rows($res)) return NULL;

  //fetching goes here
  //if there was no errors only
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
-2

If you just want to suppress warnings from a function, you can add an @ sign in front:

<?php @function_that_i_dont_want_to_see_errors_from(parameters); ?>
Computerish
  • 9,590
  • 7
  • 38
  • 49
  • 2
    -1 Warning should not be ignored. – timdev Jun 28 '10 at 00:00
  • Unless you are absolutely, completely, unequivocally convinced the error your function is throwing is harmless, this is a really bad practice. When the function fails, there's no way to identify the issue, because any helpful error message is not displayed. – Chris Henry Jun 28 '10 at 00:08
  • That's like turning off your check engine light. Perhaps figuring out why it's on and fixing it might be a better approach? – Dan Heberden Jun 28 '10 at 00:09
  • Oops, I mis-read the question. I thought he was explicitly asking how to hide the error. – Computerish Jun 28 '10 at 01:02