MySQLi prepared statement returning false

大兔子大兔子 提交于 2019-12-07 06:11:19

问题


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

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.


回答1:


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

and some mysql error should be displayed.



来源:https://stackoverflow.com/questions/8702241/mysqli-prepared-statement-returning-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!