php - 准备好的语句 - 行数

标签 php mysqli prepared-statement

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

相关文章:

php - 条件 SQL WHERE 语句

php - 何时调用 mysqli::close

php - 如何从 mysql* 升级到 mysqli*?

php 101 DateTime 使用 atom 格式

php - fatal error : Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation"

php - PHP MySQLi输出问题

mysql - 存储过程中 'where clause' 中的未知列

javascript - 影响布局的新行

php - 从 Twitter 请求特定信息

php - MySQLi 从有限制的两个表中选择