gpt4 book ai didi

php - 准备好的语句 - 行数

转载 作者:IT老高 更新时间:2023-10-29 00:21:35 29 4
gpt4 key购买 nike

我只是想弄清楚如何确定行数,然后使该数字显示在 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 ,像这样:

$stmt->store_result();
$stmt->num_rows;

但是,我正在运行,当我将代码放入其中时,页面出现错误。我什至无法进入下一步如何显示行数

在计算准备好的语句中的行数方面我遗漏了什么,然后我将如何用 <?php echo '# rows: '.$WHATGOESHERE;?> 显示它?

最佳答案

num_rows 返回数字,你必须将它存储在一个变量中。

/*.....other code...*/
$numberofrows = $stmt->num_rows;
/*.....other code...*/

echo '# rows: '.$numberofrows;

所以完整的代码应该是这样的:

$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();
$stmt -> store_result();

/* Bind results */
$stmt -> bind_result($testfield1, $testfield2, $testfield3);

/* Fetch the value */
$stmt -> fetch();
$numberofrows = $stmt->num_rows;

/* Close statement */
$stmt -> close();

echo '# rows: '.$numberofrows;

关于php - 准备好的语句 - 行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726023/

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