Jump to content


Photo

Looking for Alternate Code


  • Please log in to reply
6 replies to this topic

#1 leeish

leeish
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 September 2006 - 06:25 PM

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


#2 mainewoods

mainewoods
  • Members
  • PipPipPip
  • Advanced Member
  • 685 posts
  • LocationMaine

Posted 12 September 2006 - 06:40 PM

Like this:
if (!$records = mysql_query( "SELECT * FROM extra_logros WHERE date = '$today' AND sector_id = '$sect_id' AND ward_id = $ward_id; "))
-notice the '!' in front of $records.

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 12 September 2006 - 06:40 PM

Try splitting the code rather than nesting...

$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
}

Regards
Rich
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 leeish

leeish
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 September 2006 - 06:46 PM

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.

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 September 2006 - 06:57 PM

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

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 fix the problem, but it will help us debug further. let me know the outcome of this ;-)
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 leeish

leeish
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 September 2006 - 08:25 PM

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:

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

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

#7 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 September 2006 - 01:12 PM

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users