Reputation: 5409
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.
Upvotes: 5
Views: 7160
Reputation: 1010
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.
Upvotes: 11