准备的语句-行数 [英] Prepared Statements - Number of Rows
问题描述
我正在学习准备好的语句,并尝试使用产生多行结果的查询.现在,我只是想弄清楚如何确定行数,然后将该行数显示在html中.
I'm learning about prepared statements and trying to work with a query that yields multiple rows of results. Right now, I'm just trying to figure out how to determine the number of rows and then make that number display in the html.
我准备好的陈述如下:
if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ?ORDER BY id ASC"))
{
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("i", $id);
$stmt -> execute();
/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);
/* Fetch the value */
$stmt -> fetch();
/* Close statement */
$stmt -> close();
}
我了解我应该先保存结果,然后使用num_rows
,如下所示:
I understand that I'm supposed to first save the results, then use num_rows
, like this:
$stmt->store_result();
$stmt->num_rows;
但是,当我将代码放入其中时,我正在运行并出现页面错误.我什至无法进入如何显示行数的下一步
However, I'm running and issue with the page bugging out when I put that code in there. I haven't even been able to get to the next step of how to display the number of rows
所以,问题是:在计算准备好的语句中的行数方面,我缺少什么,然后如何用<?php echo '# rows: '.$WHATGOESHERE;?>
So, the question is: What am I missing in terms of calculating the number of rows inside the prepared statement, then how would I display it with a <?php echo '# rows: '.$WHATGOESHERE;?>
谢谢!
推荐答案
num_rows
返回数字,您必须将其存储在变量中.
num_rows
returns the number, you have to store it in a variable.
/*.....other code...*/
$numberofrows = $stmt->num_rows;
/*.....other code...*/
echo '# rows: '.$numberofrows;
因此完整的代码应该是这样的:
So full code should be something like this:
if($stmt = $mysqli -> prepare("SELECT field1, field2, field3 FROM table WHERE id= ? ORDER BY id ASC"))
{
/* Bind parameters, s - string, b - blob, i - int, etc */
$stmt -> bind_param("i", $id);
$stmt -> execute();
/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);
/* Fetch the value */
$stmt -> fetch();
$numberofrows = $stmt->num_rows;
/* Close statement */
$stmt -> close();
}
echo '# rows: '.$numberofrows;
这篇关于准备的语句-行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!