繁体   English   中英

MySQLi num_rows 返回 0

[英]MySQLi num_rows returns 0

这是我的代码

$stmt = $conn->mysqli->stmt_init();
$stmt = $conn->mysqli->prepare('SELECT Username, EmailVerified, Blocked FROM user WHERE Email = ? AND SLANumber = ? AND Password = ?');
$stmt->bind_param('ssb', $_POST['EmailID'], $_POST['SLANumber'], $_POST['Password']);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
if($result->num_rows == 0){
    echo 'No rows found';
}
else{
    // Continue processing here
    .....
}

代码总是回显No rows found 一两天前,它工作正常。

正如预期的那样,直接运行查询会给出所需的结果。

代码有什么问题?

不要在同一个语句中同时使用 store_result 和 get_result。

使用带有“num_rows”、“bind_result”和“fetch”的 store_result 方法。

对于 get_result 方法,使用“affected_rows”和“fetch_array”。 您仍然可以在收入 get_result 方法中使用“num_rows”属性,如下所示。

$stmt->execute();
// $stmt->store_result();
$result = $stmt->get_result();
if(result->num_rows == 0){
  ...
}

要么

$stmt->execute();
// $stmt->store_result();
$result = $stmt->get_result();
if($stmt->affected_rows == 0){
  ...
}

要解决您的问题,请删除此行:

$stmt->store_result();

问题是您使用了两种相互冲突的方法。

$stmt->store_result();
$result = $stmt->get_result();

这两种方法都会获取结果。 store_result()在内部获取结果并将它们存储在语句对象中。 我建议尽可能避免使用这种方法。 很难使用。 get_result()获取结果并将它们保存在一个单独的对象中。 一旦从 MySQL 获取结果,就无法再次获取它们。 一次只使用其中一种方法

在您的情况下,您首先将结果存储在mysqli_stmt ,然后将一个空结果集提取到$result $result将包含 0 行,因为所有记录都已存储在语句中。 要获取存储在那里的行数,请使用$stmt->num_rows

mysqli_stmtmysqli_result类都有num_rows属性。 关键是使用合适的。

num_rowsmysqli_stmt的属性,而不是结果资源的属性。 所以你应该这样做:

$result = $stmt->get_result();

// Also check strict comparison against int 0,
// to avoid incorrect equality with boolean FALSE
if($stmt->num_rows === 0){
    echo 'No rows found';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM