简体   繁体   English

mysqli编写语句num_rows函数

[英]mysqli prepared statement num_rows function

So it is my first day working in MYSQLi(converting over from mysql), and I am having trouble with my login script where I check for duplicate emails: 所以这是我在MYSQLi工作的第一天(从mysql转换),我在登录脚本时遇到问题,我检查了重复的电子邮件:

Here is what i've got: 这是我得到的:

$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();
}

I keep returning 0 rows or an empty variable when echoing $nrows1. 当回显$ nrows1时,我一直返回0行或空变量。 When I enter the query directly in sql, it works fine. 当我直接在sql中输入查询时,它工作正常。 I seem to be following the documents and have tinkered a bunch with it. 我似乎正在关注这些文件,并用它修补了一堆。

I also used the affected rows function, but I do not believe that is appropriate for a SELECT statement, correct? 我也使用了受影响的行函数,但我不相信这对SELECT语句是否合适,对吗?

Sincere thanks for any help, it is greatly appreciated. 真诚感谢您的帮助,非常感谢。

After executing, you need to use mysqli_stmt_store_result() to get the result set, so it will know how many rows there are. 执行后,您需要使用mysqli_stmt_store_result()来获取结果集,因此它将知道有多少行。 Also, you should apply $num_rows to the statement, not the connection. 此外,您应该将$num_rows应用于语句,而不是连接。

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

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

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