8

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

7
  • 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. Commented Jun 27, 2010 at 23:58
  • Providing the code itself might be helpful. Commented Jun 27, 2010 at 23:58
  • Sounds like you have a mysql error. Check the output of mysql_error() Commented Jun 28, 2010 at 0:09
  • Yeah , I had an error in my sql query, I checked with mysql_erro() Commented Jun 28, 2010 at 0:11
  • Dup of mysql_fetch_assoc error, can't seem to figure out what the problem is Commented Jan 14, 2012 at 6:06

6 Answers 6

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.

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

10 Comments

die() is the worst method of error handling. especially with exposing mysql error to the user. Use throw or return instead.
It beats no error handling. And this is example code. But editing just for you!
And obviously on a production site, whatever catches that exception should never display the message to the user.
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 be sure to make it //handle the error or the col will get you
|
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.

4 Comments

I wouldn't recommend using the error suppression operator @ like that. It's better to write code which handles errors, rather than ignore them silently.
that @ again. NEVER use that symbol in your code!
I agree. It isn't good style.
Glad to see an answer that checks the $result before relying on it :) - but the @? I'd remove that line all together from the answer.
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.

Comments

0

The proper syntax is (in example):

$query = mysql_query('SELECT * FROM beer ORDER BY quality');
while($row = mysql_fetch_assoc($query)) $results[] = $row;

Comments

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

Comments

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

4 Comments

-1 Warning should not be ignored.
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.
That's like turning off your check engine light. Perhaps figuring out why it's on and fixing it might be a better approach?
Oops, I mis-read the question. I thought he was explicitly asking how to hide the error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.