如何判断mysqli_query是否没有返回结果/ out of dieing

时间:2010-06-29 01:43:50

标签: php mysql

我从id列表中抓取time。并非每个id都有与之相关的时间。 如何判断是否没有结果(表中还没有EG ID)然后没有出现大量Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 9 in /home/aslum/...错误......

$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>";

2 个答案:

答案 0 :(得分:1)

if(mysql_num_rows($result) > 0) {
  // do something
}

答案 1 :(得分:1)

使用mysql_errnomysql_num_rows提供更多信息,这是一种稍微详细一点的方法,可以查看出错的地方。

$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' :(";
}

(另外,如果可以,请使用mysqliPDO扩展名而不是mysql。)