简体   繁体   English

我如何确定mysql_query是否死掉而没有返回结果

[英]How do I find out if no results are returned from a mysql_query w/out dieing

I am grabbing the time from a list of ids. 我从ID列表中抢time Not every id has a time associated with it. 并非每个ID都有与之关联的时间。 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... 如何判断是否没有结果(表中还没有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>";
if(mysql_num_rows($result) > 0) {
  // do something
}

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. 这是使用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' :(";
}

(Also, please use the mysqli or PDO extensions instead of mysql if you can.) (此外,请尽可能使用mysqliPDO扩展而不是mysql。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM