Damian Doman
Damian Doman

Reputation: 532

PHP/MYSQL - Only one element from row can be shown, possible issue?

I'm trying to construct login verifying system in PHP/MySQLi. The _POST data is being sent by AJAX request/jQuery, but I have simplified the code as much as possible to allow you to simulate the query even without the unnecessary data. I have also omitted the string verifiers(FILTER_SANITIZE_STRING etc.) for the sake of simplifying

The code (simplified) looks as follows:

 $email = "[email protected]";
$haslo = "averyhardpassword12345";

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $output = json_encode(array('type'=>'error', 'text' => 'Email address is not valid.'));
    die($output);
}
if(strlen($haslo)<3 || strlen($haslo)>20){
    $output = json_encode(array('type'=>'error', 'text' => 'Password must have between 3 and 20 characters.'));
    die($output);
}

    $mysqli = new mysqli("localhost", "myDbUsername", "myDbPW", "myDbName");

    $usercheckquery = "SELECT * FROM users WHERE email='$email'";
    $result = $mysqli->query($usercheckquery);
    while($row = mysqli_fetch_assoc($result))
    $emailbaza = $row['email'];
    $haslobaza = $row['haslo'];
    echo $haslobaza;
    echo $emailbaza;
    if (password_verify($haslo, $haslobaza) && $email == $emailbaza && !empty($data)) {
        $output = json_encode(array('type'=>'message', 'text' => 'Zalogowano...'));
        die($output);
    } else {
        $output = json_encode(array('type'=>'error', 'text' => 'Podany email lub hasło są nieprawidłowe.'));
        die($output);
    }

The problem is - i can't manage to find out what's wrong with this code. My final if statement never returns true, also I've found out that I can echo $row['email'], but when i echo $row['haslo'] it returns nothing.

Hope somebody could show me where have I made a mistake.

EDIT:

Oh, and haslo is saved in db in haslo column and is generated through password_hash($haslo, PASSWORD_BCRYPT);. Haslo column is varchar(255).

Upvotes: 1

Views: 50

Answers (1)

Scuzzy
Scuzzy

Reputation: 12322

your while loop need braces to encompass all your usage of $row

Otherwise $row will eventually return null and $haslobaza will also be null (it would be throwing a index error at this point)

while($row = mysqli_fetch_assoc($result))
{
  $emailbaza = $row['email'];
  $haslobaza = $row['haslo'];
}

Upvotes: 3

Related Questions