【问题标题】:mysqli prepared statement num_rows function [duplicate]mysqli准备好的语句num_rows函数[重复]
【发布时间】:2014-12-15 22:38:59
【问题描述】:

所以这是我在 MYSQLi 中工作的第一天(从 mysql 转换),我在检查重复电子邮件时遇到了登录脚本问题:

这是我得到的:

$email = mysqli_escape_string($_POST['email']);

$stmt=$mysqli->prepare("SELECT username from users WHERE email=?");
$stmt->bind_param('s',$email);
$stmt->execute(); 
$nrows1=$mysqli->num_rows;
echo $nrows1;
$stmt->close();

if ($nrows1>0) {
    $_SESSION['loggedin'] = false;
    $_SESSION['error'] = "Our records indicate that there is already an account registered with that email. Please use the 'forgot your password' link below if you cannot remember your password.";
    header('Location: registration.php');
    echo "running insisde duplicate email";
        exit();
}

在回显 $nrows1 时,我一直返回 0 行或一个空变量。当我直接在sql中输入查询时,它工作正常。我似乎在关注这些文件,并且对它做了很多修改。

我也使用了受影响的行函数,但我认为这不适用于 SELECT 语句,对吗?

衷心感谢您的帮助,非常感谢。

【问题讨论】:

  • 如果你使用bind_param,我认为你不需要转义参数。
  • 谢谢。是的,这实际上也在抑制它。

标签: php mysql mysqli


【解决方案1】:

执行后,需要使用mysqli_stmt_store_result()来获取结果集,这样才会知道有多少行。此外,您应该将$num_rows 应用于语句,而不是连接。

$stmt->execute();
$stmt->store_result();
$nrows1 = $stmt->num_rows;

【讨论】:

  • 非常感谢。我也想知道双重转义,这实际上也是问题的一部分。存储的结果效果很好。
  • 没错。如果使用bind_param,则无需转义数据。