Evaluating a Result Set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

Evaluating a Result Set

Post 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?
jeffrydell
Forum Commoner
Posts: 77
Joined: Thu Jan 17, 2008 4:39 pm
Location: Menasha, WI

Re: Evaluating a Result Set

Post by jeffrydell »

FOUND IT! ... Semicolon after the IF in line 9:
jeffrydell wrote:
if ($num == 0);
{
    die ("Not a valid client lookup.");
}
   
cavemaneca
Forum Commoner
Posts: 59
Joined: Sat Dec 13, 2008 2:16 am

Re: Evaluating a Result Set

Post 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"
Post Reply