Reputation: 744
I have made a prepared statement, supposed to login a user, everything works just fine, but I'm facing a problem with bind_result()
. What I mean is that it is not working at all and I can't get the query's result and assign it to the SESSION
Here is my code:
session_start();
$sesUsername = "";
$sesCid = "";
$sesFirstName = "";
$sesLastName = "";
$mysqli = new mysqli($host_l, $username_l, $password_l, $dbName_l);
$loginQuery = "SELECT username, cid, first_name, last_name FROM Users WHERE username=? and password=?";
if($stmt = $mysqli->prepare($loginQuery)){
$stmt->bind_param('ss',$usernameEsc, $passwordEsc);
$stmt->execute();
/* Store the result (to get properties) */
$stmt->store_result();
/* Get the number of rows */
$num_of_rows = $stmt->num_rows;
/* Bind the result to variables */
$stmt->bind_result($sesUsername, $sesCid, $sesFirstName, $sesLastName);
if($num_of_rows == 1){
$_SESSION['username']= $sesUsername; // This is not working
return true;
}
else {
http_response_code(401);
}
/* free results */
$stmt->free_result();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
The code redirects me to the members area, but in there the session variable username
is empty.
Why I can't get it working? I know that I'm missing something really small, but as a newbie, I can't spot the problem.
Upvotes: 1
Views: 61
Reputation: 4110
try this:
$stmt->bind_result($sesUsername, $sesCid, $sesFirstName, $sesLastName);
while ($stmt->fetch()) {
$_SESSION['username']= $sesUsername;
}
Upvotes: 1
Reputation: 780974
You have to do:
$stmt->fetch();
to get the results into the variables.
Upvotes: 4