簡體   English   中英

SQL計數查詢返回的行數預設為1,而不是實際的行數

[英]SQL count query returning number of rows preset as 1 instead of actual number of rows

下面是從中獲取數據庫表中許多行的PHP代碼。 但是以下代碼始終將行數返回為1。 請幫助我解決問題,以從數據庫中獲取正確的行數。

$db=mysqli_connect("localhost","root","","test");               
     echo "<div class='row text-center col-lg-12' align='center'>";     
    $cmd="SELECT COUNT(*) FROM product WHERE product_name LIKE '%$search_query%'";

    $result = mysqli_query($db, $cmd);
    $total = mysqli_num_rows($result);

如果您執行SELECT COUNT(*)您將返回到第一行的計數值所在的行。 因此,您需要像這樣獲得COUNT()值:

$result = mysqli_query($db, $cmd);
$row = mysqli_fetch_array($result);
$total = $row[0];

這應該工作:

$db=mysqli_connect("localhost","root","","test");               
     echo "<div class='row text-center col-lg-12' align='center'>";     
    $cmd="SELECT * FROM product WHERE product_name LIKE '%$search_query%'";

    $result = mysqli_query($db, $cmd);
    $total = mysqli_num_rows($result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM