Jesus Velarde Jesus Velarde - 29 days ago 11
MySQL Question

I cannot get msqli_result to print

My login system works fine so far. I can put the correct username and password and it will login but not vice versa. The only thing that does not work correctly is the status check. If the user has a status of '0' it should not login. So I tried to print out the status and nothing showed. Then I did the same with the password_hash and I get the same result (which is weird since password_verify() has no issue). The user entered password does print so this must be a mysqli_result thing. Any help?

include_once($_SERVER['DOCUMENT_ROOT'] . "/php_scripts/db_conx.php");

function CheckLogin($username, $password, $db_conx){
time_nanosleep(0, 100000000);
$userQuery = $db_conx->prepare("SELECT * FROM users WHERE username = ?");
$userQuery->bind_param('s', $username);
$userQuery->execute();
$userResult = $userQuery->get_result();

if($userResult->num_rows == 1){
$password_hash = $userResult->fetch_assoc()['password'];

$status = mysqli_fetch_assoc($userResult)['status']; // $status turns out null
echo($status); // Prints nothing
printf("%s\n", $userResult->fetch_assoc()['password']); // Prints nothing
print_r($userResult->fetch_assoc()); // Prints nothing

print($password); //Prints the password correctly

if(password_verify($password, $password_hash) && $status != '0'){
//password_verify() works like a charm
//Status check is the only thing that does not work
return true;
} else{
return false;
}

}
return false;
}


The user has a status of '0' so it should not login.

Answer Source

You're advancing the cursor 4 positions but probably only have 1 row. Use fetch only once and assign it.

$row = $userResult->fetch_assoc();

then you can assign your variables.

$password_hash = $row['password'];
$status = $row['status'];

and they will be accessible.

You also could just check the status in the query:

SELECT * FROM users WHERE username = ? and status <> 0

or

SELECT * FROM users WHERE username = ? and status = 1