php - num_rows 属性永远无法预测

标签 php mysql mysqli

我真的厌倦了这个 num_rows 属性。首先,我在没有 store_result() 的情况下使用它,但它无法正常工作。然后我发现我应该将结果存储在本地以便找出记录数。有效。现在它再次停止工作。

当我检查属性 RequestedId 时,它确实返回了一个值。但是,num_rows 表示。我还缺少什么?

  $query="select RequestedId from friend_request where RequestingId=?";
  $stmt=$mysqli->stmt_init();
  $stmt->prepare($query);
  $stmt->bind_param('i',$requestinguserid);
  $stmt->execute();
  $stmt->bind_result($RequestedId);
  $stmt->fetch();
  $stmt->store_result();
  if($stmt->num_rows > 0)
  {

    return $RequestedId;
 }
 else{
return 0;

}

最佳答案

从本页的解释来看:http://php.net/manual/en/mysqli-stmt.store-result.php

在获取之前需要调用store_result 函数。

$stmt->execute();

/* store result */
$stmt->store_result();

printf("Number of rows: %d.\n", $stmt->num_rows);

/* free result */
$stmt->free_result();

您应该能够在释放结果之前阅读结果。

关于php - num_rows 属性永远无法预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30170334/

相关文章:

mysql - 如何在 sequelize 上使用 mysql REPLACE 执行搜索?

mysql - 删除sql字段中的空格

php - 在 MySQL 和 PHP 中查询数据库中的所有表

php - MySQL 查询不会使用 MySQLi 执行

php - curl 的大小或长度限制是多少?

php - 在 Zend Framework 中缓存 PDOStatement 对象

php - 如何在linux中使用PHP调用另一个CLI.exe进程I/O?

php - 下载 PDF - 失败 : Network error, PHP-FPM 和 Nginx

MySQL 月度报告日期

php - 第一个参数必须是字符串