[英]SQL count query returning number of rows preset as 1 instead of actual number of rows
Below is PHP code from where I'm fetching a number of rows in a database table. 下面是从中获取数据库表中许多行的PHP代码。 But following code is returning a number of rows as 1 always. 但是以下代码始终将行数返回为1。 Please help me solve the problem to fetch the correct number of rows from the database. 请帮助我解决问题,以从数据库中获取正确的行数。
$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);
If you do a SELECT COUNT(*)
you will get a row back where the counted value is in the first column. 如果您执行SELECT COUNT(*)
您将返回到第一行的计数值所在的行。 So you need to get the COUNT()
value like this: 因此,您需要像这样获得COUNT()
值:
$result = mysqli_query($db, $cmd);
$row = mysqli_fetch_array($result);
$total = $row[0];
This should work: 这应该工作:
$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.