我尝试使用准备好的语句统计查询的返回行。
类似这样的东西:
$q = "SELECT name, address, contact FROM members";
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
但在执行此查询时,我总是得到0行。我使用mysql客户端ant测试了它,然后得到了6行返回。
有人能告诉我这个怎么了吗?
谢谢。
在尝试存储结果之前,您需要调用mysqli_stmt_execute
来实际获取结果集。
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_execute($stmt); // <--------- currently missing!!!
mysqli_stmt_store_result($stmt);
$rows = mysqli_stmt_num_rows($stmt);
您必须执行查询
$q = "SELECT name, address, contact FROM members";
if ($stmt = mysqli_prepare ($dbc, $q)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
mysqli_stmt_free_result($stmt);
mysqli_stmt_close($stmt);
}
您需要对语句句柄调用execute((,现在您只准备查询。
$q = "SELECT name, address, contact FROM members";
$stmt = mysqli_prepare ($dbc, $q);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
// Get the number of rows returned:
$rows = mysqli_stmt_num_rows($stmt);
echo $rows;
我担心您的表Members中是否真的有记录
$rows = mysqli_num_rows($stmt);
//or
$rows = mysql_num_rows($stmt);
这对我来说很成功,因为我的表中只有5张记录,所以我已经返回了5张。