Error returning bank statement in MySQLi

0

Well, I'm doing a login system in PDO I do not 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 the return of echo $ result ['code'] is always empty nothing appears.

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

Does anyone have an idea how to solve it?

    
asked by anonymous 23.02.2017 / 03:15

1 answer

1

According to the manual ( link ), you should use bind_result to bind in the columns. fetch will put the values of the columns in the variables that were binded . The variable $ result gets a boolean, so if it had more than one record it would need to iterate with a while.

When using -> fetch indicates that you are using object-oriented mode, to get as array you should use mysqli_fetch_assoc( mysqli_result $result ) ( link )

Follow your modified code to stay the way the manual tells you about 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();
    
23.02.2017 / 13:13