MySQLi prepared a statement returning false

I am trying to run multiple queries in 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 starts, 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 checked that the database fields exist, so this is not the case. The first query works, it just seems like the second one that doesn't. I myself executed the request in phpMyAdmin and successfully created the result set, so I really don't know what happened.

+5
source share
1 answer

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 display.

+9
source

All Articles