mysqli准备语句num_rows函数 [英] mysqli prepared statement num_rows function

查看:46
本文介绍了mysqli准备语句num_rows函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

这就是我所拥有的:

$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中输入查询时,它可以正常工作.我似乎正在关注这些文档,并对其进行了一些修改.

I keep returning 0 rows or an empty variable when echoing $nrows1. When I enter the query directly in sql, it works fine. I seem to be following the documents and have tinkered a bunch with it.

我还使用了受影响的行功能,但我认为这不适合SELECT语句,对吗?

I also used the affected rows function, but I do not believe that is appropriate for a SELECT statement, correct?

真诚的感谢您的帮助,我们将不胜感激.

Sincere thanks for any help, it is greatly appreciated.

推荐答案

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

After executing, you need to use mysqli_stmt_store_result() to get the result set, so it will know how many rows there are. Also, you should apply $num_rows to the statement, not the connection.

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

这篇关于mysqli准备语句num_rows函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆