Page 1 of 1

Evaluating a Result Set

Posted: Sun Dec 14, 2008 9:50 am
by jeffrydell
This is so basic - I feel like a complete idiot having to ask about this - but I've spent an hour trying to get this code to work right and it still won't. Can anyone here tell me WHY this script always dies with no results even though the SQL successfully brings up a row when I execute it in phpMyAdmin?

Code: Select all

    
$sql = "SELECT * FROM `table` WHERE `id` = '" . $id . "'";
$res = mysql_query($sql);
    
//  die ("$sql");       // Display the $sql for testing in phpMyAdmin
    
$num = mysql_num_rows($res);
 
if ($num == 0);
{
    die ("Not a valid client lookup.");
}
    
$Row = mysql_fetch_assoc($res);
$url = $Row['url'];
 
How do you evaluate for an empty result set to throw an error?

Re: Evaluating a Result Set

Posted: Sun Dec 14, 2008 10:20 am
by jeffrydell
FOUND IT! ... Semicolon after the IF in line 9:
jeffrydell wrote:
if ($num == 0);
{
    die ("Not a valid client lookup.");
}
   

Re: Evaluating a Result Set

Posted: Mon Dec 15, 2008 2:41 pm
by cavemaneca

Code: Select all

if ($num == 0) {
  die ("Not a valid client lookup.");
}
Trying putting it in like this. It helps stop confusion. Also, look into PEAR standards for coding. Not all of them I agree with but most of them help everyone, including yourself.
And next time you realize that you have found your solution right after posting it, just edit the first post and say something like:

"EDIT: Ooops! This is what's wrong!
blah blah code blah"