MySQLi准备好的语句返回false [英] MySQLi prepared statement returning false

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

问题描述

我正在尝试使用MySQLi在数据库上运行多个查询.这是我的代码:

I'm trying to run multiple queries on my database using MySQLi. This is my code:

$stmt = $mysqli->prepare('SELECT password FROM `users` WHERE username=? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($hashedPass);
$stmt->fetch();

/* Check the passwords match */
$pwdHasher = new PasswordHash(8, FALSE);
if(!$pwdHasher->CheckPassword($password, $hashedPass))
    exit;

$stmt = $mysqli->prepare('SELECT u_id FROM `users` WHERE username=? LIMIT 1');
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($u_id);
$stmt->fetch();

但是在运行代码时出现此错误:

But when the code is run I get this error:

Fatal error: Call to a member function bind_param() on a non-object in C:\wamp\www\ajax\login.php on line 42

我已经检查了数据库字段是否存在,所以不是.第一个查询有效,似乎只是第二个无效.我已经在phpMyAdmin中单独运行了查询,并成功生成了一个结果集,所以我真的不知道出了什么问题.

I have checked that the database fields exist, so it's not that. The first query works, it just seems to be the second one that doesn't. I've run the query on its own in phpMyAdmin and that successfully produces a result set, so I really don't know what's wrong.

推荐答案

准备返回false.尝试

prepare returns false if an error occurs. try

$stmt = $mysqli->prepare('SELECT u_id FROM `users` WHERE username=? LIMIT 1');
if ($stmt === FALSE) {
    die ("Mysql Error: " . $mysqli->error);
}

,并且应该显示一些mysql错误.

and some mysql error should be displayed.

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

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