tags:

views:

27

answers:

2

I am grabbing the time from a list of ids. Not every id has a time associated with it. How can I tell if there are no results (EG ID is not in the table yet) and then not get lots of Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 9 in /home/aslum/... errors...

$tquery = "SELECT time ".
    "FROM clicks ".
    "WHERE id LIKE '".$id."'";
$tresults = mysql_query($tquery) or die (mysql_error());
// What do I do here? 
// IF $TRESULTS == "" THEN $foobar = false else $foobar = true
$clicktime = mysql_result($tresults,0);
if ($foobar==true) echo $clicktime."<br>";
+1  A: 
if(mysql_num_rows($result) > 0) {
  // do something
}
+1  A: 

Here's a slightly more verbose way to see where things are going wrong, using mysql_errno and mysql_num_rows to provide more information.

$sh = mysql_query($sql);
if($sh === false) {
    $error = mysql_errno();
    if($error) {
    // You really should be logging this instead of just calling die.
        die(mysql_error());
    }
// Otherwise, the query just didn't return a result set for some reason...
    echo "Something bad and unexpected happened.";
}
// Now the fun part.
if(mysql_num_rows($sh) > 0) {
    echo "I got rows!";
} else {
    echo "I got nothin' :(";
}

(Also, please use the mysqli or PDO extensions instead of mysql if you can.)

Charles