tags:

views:

55

answers:

6

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

any ideas ?

edit: Thank you guys , that was very helpful

A: 

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
-1 Warning should not be ignored.
timdev
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
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
Oops, I mis-read the question. I thought he was explicitly asking how to hide the error.
Computerish
+1  A: 

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.

Riateche
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.
nickf
that @ again. NEVER use that symbol in your code!
Col. Shrapnel
I agree. It isn't good style.
Riateche
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.
Dan Heberden
+1  A: 

The proper syntax is (in example):

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

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.

sixfoottallrabbit
+3  A: 

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

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