我试图根据使用PHP的SQL查询查看其中有多少行。
我似乎能够查询数据库并从一行返回字段,但似乎无法根据相同的查询找出其中有多少行。
<?php
$host = 'localhost';
$user = 'MyUsername';
$pass = 'MyPassword';
$database = 'MyDatabase';
$con=mysqli_connect($host,$user,$pass ,$database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM MyTable WHERE test='123' AND test2='456'");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";
mysqli_close($con);
?> 它只返回屏幕上的文本行,在开始时没有行数。
正如我说过的,如果我尝试使用以下方法选择一行,这个查询将工作并返回一个值:
while($row = mysqli_fetch_array($result))
{
echo $row["test"];
}有人知道为什么它不返回行数吗?
发布于 2013-10-20 06:46:29
您正在使用MySQLi。因为您没有mysql查询,所以mysql_num_rows不返回所需的值。您必须将mysql函数替换为mysqli相等:
$num_rows = mysqli_num_rows($result);发布于 2013-10-20 06:48:26
您正在使用Mysqli,您应该使用mysqli_num_rows
$result = mysqli_query($con,从MyTable中选择*,test='123‘和test 2=’456‘);$num_rows = mysqli_num_rows($result);
如果你只想要count,那么你可以像这样直接使用count(*):
$result = mysqli_query($con,从MyTable中选择COUNT(*),test='123‘和test 2=’456‘); 回声$result;
发布于 2013-10-20 06:51:58
我会做这样的事:
$result = mysqli_query($con,"SELECT COUNT(*) FROM MyTable WHERE test='123' AND test2='456'");
echo $resulthttps://stackoverflow.com/questions/19474655
复制相似问题