2010-06-29 49 views
0

我從一個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/...錯誤的...如何查找是否沒有結果返回從一個mysql_query w/out dieing

$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
if(mysql_num_rows($result) > 0) { 
    // do something 
} 
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。)