Error when displaying bank return in Mysqli

Asked

Viewed 72 times

0

Guys, I’m doing a PDO login system I don’t understand much, but I’m going through this problem, I developed the following code below:

    $select = $con -> prepare("SELECT * FROM conta WHERE conta = ? AND senha = ?");
    $select -> bind_param("ss", $usuario->getConta(), md5($usuario->getSenha()));
    $select ->execute();
    $result = $select->fetch();

    if($result == 1)
    {
        echo $result['codigo'];
        echo 'success';
    }
    else
    {
        echo 'not_register';

    }

Only that the return of echo $result['code'] is always empty nothing appears.

and when I give a var_dump($result) appears the result of true and nothing else and when I give a print_r($result) appears the number 1...

Does anyone have any idea how to solve?

  • That ain’t no PDO. bind_param() does not allow functions to be passed, only variables.

  • 2

    And don’t invent a PHP password system like that. It’s full of braggart blog teaching how to use it, but it doesn’t make sense (in 2005 maybe it did). PHP has appropriate functions for this, which are password_hash and password_verify

1 answer

1


According to the manual ( http://php.net/manual/en/mysqli-stmt.fetch.php ), you must use bind_result to bind the columns. fetch will put the column values in the variables that were bound. The $result variable takes a Boolean, so if you had more than one record you would need to iterate with a while.

When using ->fetch indicates that you are using object-oriented mode, to pick up as array you should use mysqli_fetch_assoc( mysqli_result $result ) (https://secure.php.net/manual/en/mysqli-result.fetch-assoc.php)

Follow your modified code to stay the way the manual indicates for the object-oriented approach:

$select = $con->prepare("SELECT codigo, conta FROM conta WHERE conta = ? AND senha = ?"); 
$select->bind_param("ss", $usuario->getConta(), md5($usuario->getSenha()));
$select->execute();

$select->bind_result($codigo, $conta);  //bind nas colunas

$result = $select->fetch();

if($result)
{
    echo $codigo;       // echo $conta;
    echo 'success';
}
else
{
    echo 'not_register';
}


$select->close();
$con->close();
  • Note that I needed to change your query to pass the column names. Test the code and let’s talk around here if it’s not working accordingly.

  • It got really good my friend! It worked perfectly! Thank you

  • Good Peter! I’m happy! :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.