Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli_stmt::num_rows()返回错误的值_Php_Mysql_Mysqli - Fatal编程技术网

Php mysqli_stmt::num_rows()返回错误的值

Php mysqli_stmt::num_rows()返回错误的值,php,mysql,mysqli,Php,Mysql,Mysqli,我正在用PHP编写一个数据库处理程序类,使用mysqli类和准备好的语句。我正试图把结果打印出来。它不能马上工作,所以我决定做一些调试。我试图使用mysqli_语句类中的num_rows方法,但它始终返回0。我决定编写一小部分测试代码以使其更简单,这样我就可以看到哪里出了问题。然后,我可以返回我想要的数据,但是num_rows方法仍然返回0,即使它实际上正在选择和检索一些数据。代码如下: $mysqli = new mysqli('localhost', 'username', 'passwor

我正在用PHP编写一个数据库处理程序类,使用mysqli类和准备好的语句。我正试图把结果打印出来。它不能马上工作,所以我决定做一些调试。我试图使用mysqli_语句类中的num_rows方法,但它始终返回0。我决定编写一小部分测试代码以使其更简单,这样我就可以看到哪里出了问题。然后,我可以返回我想要的数据,但是num_rows方法仍然返回0,即使它实际上正在选择和检索一些数据。代码如下:

$mysqli = new mysqli('localhost', 'username', 'password', 'database');
if(mysqli_connect_errno())
{
  die('connection failed');
}

$statement = $mysqli->stmt_init();

$query = "SELECT name FROM table WHERE id = '2000'";
if($statement->prepare($query))
{
    $statement->execute();
    $statement->bind_result($name);
    $statement->fetch();
    $statement->store_result();
    echo $statement->num_rows();
    echo $name; 
}
else
{
    echo 'prepare statement failed';
    exit();
}
预期结果是:

1name
0name
实际结果是:

1name
0name

谁能告诉我这是为什么吗?

看起来您没有声明$name

另外,请尝试删除bind_result和fetch,使其内容如下:

$statement->execute();

$statement->store_result();

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

看起来您没有声明$name

另外,请尝试删除bind_result和fetch,使其内容如下:

$statement->execute();

$statement->store_result();

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

我想知道num_行是否相对于当前结果集进行报告。在获取数据之前,尝试捕获num_行。e、 g

if($statement->prepare($query))
{
    $statement->execute();
    $statement->store_result();
    echo $statement->num_rows();
    $statement->bind_result($name);
    $statement->fetch();
    echo $name; 
}

这有什么影响吗?

我想知道num_行是否相对于当前结果集进行报告。在获取数据之前,尝试捕获num_行。e、 g

if($statement->prepare($query))
{
    $statement->execute();
    $statement->store_result();
    echo $statement->num_rows();
    $statement->bind_result($name);
    $statement->fetch();
    echo $name; 
}

这有什么影响吗?

num\u rows不是一个方法,它是一个属性。

num\u rows不是一个方法,它是一个属性。

为了能够使用mysqli\u stmt::num\u rows,您需要将所有行提取到PHP中。获取所有内容有两种方法:使用store_result缓冲或使用fetch手动获取所有行

在您的情况下,您已经通过调用fetch一次开始手动获取。当另一个提取过程正在进行时,无法调用store_result。调用store_result失败,出现错误*

$statement->fetch();
$statement->store_result(); // produces error. See $mysqli->error;
echo $statement->num_rows();
最简单的解决方案是交换调用这两个方法的顺序

$statement->store_result();
$statement->fetch(); // This will initiate fetching from PHP buffer instead of MySQL buffer
echo $statement->num_rows(); // This will tell you the total number of rows fetched to PHP

*由于PHP中存在错误,此错误不会在异常错误报告模式中触发异常。错误消息只能通过mysqli_error函数或其相应属性查看。

为了能够使用mysqli_stmt::num_行,您需要将所有行提取到PHP中。获取所有内容有两种方法:使用store_result缓冲或使用fetch手动获取所有行

在您的情况下,您已经通过调用fetch一次开始手动获取。当另一个提取过程正在进行时,无法调用store_result。调用store_result失败,出现错误*

$statement->fetch();
$statement->store_result(); // produces error. See $mysqli->error;
echo $statement->num_rows();
最简单的解决方案是交换调用这两个方法的顺序

$statement->store_result();
$statement->fetch(); // This will initiate fetching from PHP buffer instead of MySQL buffer
echo $statement->num_rows(); // This will tell you the total number of rows fetched to PHP

*由于PHP中存在错误,此错误不会在异常错误报告模式中触发异常。错误消息只能通过mysqli_error函数或其相应属性查看。

事实上,这就是问题所在。为了让num_rows返回正确的值,必须在获取之前调用store_result;获取数据会增加内部行计数器,因此您可以执行类似while$statement->num_rows{/*do stuff*/}的操作。我坚信@StevenOxley的评论就是这个问题的答案。事实上,这就是问题所在。为了让num_rows返回正确的值,必须在获取之前调用store_result;获取数据会增加内部行计数器,因此您可以执行诸如while$statement->num_rows{/*do stuff*/}之类的操作。我坚信@StevenOxley的评论就是这个问题的答案。