如何知道查詢結果是0條記錄

來源:互聯網
上載者:User
怎麼知道查詢結果是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是返回一個數組
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.