gpt4 book ai didi

PHP mysqli_num_rows() 函数不工作

转载 作者:行者123 更新时间:2023-12-04 22:28:49 25 4
gpt4 key购买 nike

我已经复制了用于执行 mysqli 行计数函数的 W3 学校代码,但是当我输入我的变量时,返回的结果总是“1”。我已经通过直接将 SQL 代码输入到 phpMyAdmin 来检查它,它返回了正确的结果。

这是我的代码:

<?php
$con = mysqli_connect("x", "x", "x", "x");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT COUNT(*) FROM `Existing_Bookings`";

if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}

mysqli_close($con);
?>

我只是想知道是否有人能看出这背后有什么明显的原因吗?我的服务器运行的是 PHP 5.6,这就是为什么我确保我使用的是 mysqli,而不是 mysql。

最佳答案

如果表中有任何行,选择 count(*) 将始终只返回一行(如您所见)。该行的内容包含您查询的实际计数:

$sql="SELECT COUNT(*) FROM `Existing_Bookings`";

if ($result = mysqli_query($con,$sql)) {

// Return the number of rows in result set
$row = mysqli_fetch_array($result, MYSQLI_NUM);

printf("Table has %d rows.\n", $row[0]);

// Free result set
mysqli_free_result($result);
}

mysqli_close($con);

关于PHP mysqli_num_rows() 函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59592289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com