Jump to content

Recommended Posts

Throught our website many times we like to check to see if information exists in a table with a piece of code like this...

if (mysql_num_rows(mysql_query( "SELECT * FROM extra_logros WHERE date = '$today' AND sector_id = '$sect_id' AND ward_id = $ward_id; ")) < 1)

I am checking to see if there is a row in the table that matches the criteria.  If it does not exist then the next line of code creates and if it does exist then obviously I am doing something with the code.  With PHP 4 I am not sure if we had errors off or what, but all went well.  On our new server with PHP 5, the code still works and all the information gets passed, BUT I get a nice little error at the top of the screen that says:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/solomon/lib/Write.php on line 545

Ok, fine I understand that it's mad that the result is NULL I guess, but that's fine with me.  I want it to be NULL.  My qestuion is:  Is there a different way to write the code so I don't get the error? OR Can I turn off the Warnings for situations such as this?  The page works, I just get a little stupid error at the top of the screen. >:(
Link to comment
https://forums.phpfreaks.com/topic/20538-looking-for-alternate-code/
Share on other sites

Try splitting the code rather than nesting...

[code]
$sql = "SELECT * FROM extra_logros WHERE date = '$today' AND sector_id = '$sect_id' AND ward_id = $ward_id";
$result = mysql_query($sql);
if (mysql_num_rows($result) <1){
  // Do stuff
}
[/code]

Regards
Rich
I have tried both of those.  No go.  The code works correctly, it pulls a nothing.  It should pull nothing.  I don't understand why I get the error.  The only thing I can tell is that there is something turned on in the PHP settings to show lamer errors.
a couple options: first (and the least recommended), you can simply attach a "@" to the front of your mysql_num_rows() to suppress warnings for that query. the down side to this is that if for some reason your error runs deeper, you don't want it suppressed. a better option is to break down the code and find out what's causing the error. it's usually because something in the query doesn't contain the value you think it does and the query is actually returning an error. try this and see where your error is:
[code]
<?php
$q =  "SELECT * FROM extra_logros WHERE date = '$today' AND sector_id = '$sect_id' AND ward_id = $ward_id";
$sql = mysql_query($q) or die(mysql_error() . "<br />SQL: $q");
if (mysql_num_rows($sql) < 1)
?>
[/code]

what this will do is help you narrow down exactly what's causing the error. if the script dies, it will output the query and you can dissect it. also, notice that i've removed the space and semicolon from within your query statement since it's not needed.

this will not [b]fix[/b] the problem, but it will help us debug further. let me know the outcome of this ;-)
Thanks for the help.  I never thought to break apart the code like that.  It turns out that with the code written on the site we have a ton of functions that of course use varaibles passed through them from other pages.  Well, a lot of times we run a function without passing the argument through it even though the function is written with it.  For example we have a:

[code]function dothis($a)
{
  if ($a != NULL){
    DO THIS
  } else {
    DO THIS OTHER THING
  }
}
[/code]

I didn't write most of the code for the website, but aparently they trying to save their brains and make two functions in one.  I guess it still serves it's purpose but PHP and MYSQL don't like it too much.  My option now is to go through and fix it all, I am guessing recommended, or leave it and supress errors.

Breaking apart the code helped me see that I was not passing through an argument which was the $ward_id.  My code was skipping it of course because it didn't find a row to match and went to the second option.  Thanks for the help of breaking apart the code.  It was a big help.

Why does my current server not show the errors.  It PHP 5 better at finding them.  The same page on the server with PHP 4 doesn't return any warnings.  Maybe it has different settings and they are turned off? 

Thanks anyway...
yes, there are a lot of settings in the php.ini you can set flags on or off for error suppression. i personally like to turn them all the way up and make sure my code doesn't throw any (at least for development). error_reporting i like to set to E_ALL. i think that with PHP 5, it may have it turned up a bit by default, but i'm not positive about that.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.