怎麼知道查詢結果是0條記錄
我想在插入記錄之前先查詢資料庫裡面有沒有這條記錄,實際上我資料庫表裡面並沒有滿足certificatetype='hdfj'的記錄
$sql="select * from customers where certificatetype='hdfj'";
$result=mysql_query($sql);
if(!$result)
die mysql_error();
else
{
//這兒應該怎麼做才知道資料庫裡面有沒有這條記錄呢?
//為什麼我下面這兩條語句都會輸出1呢?
echo count(mysql_fetch_row($result));
echo count($result);
}
------解決方案--------------------
mysql_num_rows()擷取結果集的行數。
之所以返回1,是由於count()的特殊。 如你所說資料庫沒有這條記錄的話,mysql_fetch_row($result) 返回的就應該是false。而
$result = count(false);
// $result == 1
------解決方案--------------------
echo mysql_num_rows($result) //返回查詢的行數。
------解決方案--------------------
探討
mysql_num_rows()擷取結果集的行數。
之所以返回1,是由於count()的特殊。 如你所說資料庫沒有這條記錄的話,mysql_fetch_row($result) 返回的就應該是false。而
$result = count(false);
// $result == 1
------解決方案--------------------
PHP code
//這兒應該怎麼做才知道資料庫裡面有沒有這條記錄呢?echo mysql_affected_rows();//或者echo mysql_num_rows($result);
------解決方案--------------------
count()函數裡第一個參數應該是array or object."If var is not an array or ......,1 will be returned."參考PHP手冊:
http://cn2.php.net/manual/en/function.count.php
------解決方案--------------------
mysql_num_rows 是計數
mysql_fetch_array he mysql_fetch_rows是返回一個數組