PHP login Mysqli Error

Asked

Viewed 74 times

1

I’m developing a platform and on the Login I have a problem. The page index.php has the login form(Email and password). I’m having trouble comparing the entered data with the BD data.

require_once 'db_connect.php';
session_start();


//$erros = array();
$email = mysqli_escape_string($connect, $_POST['email']);
$senha = mysqli_escape_string($connect, $_POST['senha']);

print_r($email);
print_r($senha);

if(!empty($email) and !empty($senha)):
    $sql = "select login from utilizador where login = '$email'";
    $resultado = mysqli_query($connect, $sql);
    print_r($resultado);
    if(mysqli_num_rows($resultado)>0):

        $sql = "SELECT * FROM utilizador WHERE login = '$email' and password = (md5('$senha'))";

        $resultado = mysqli_query($connect, $sql);
         print_r($resultado);
    endif;

The result obtained is this:

     [email protected]_result Object ( [current_field] => 0 

     [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0 )

     mysqli_result Object ( [current_field] => 0 [field_count] => 4

     [lengths] => [num_rows] => 0 [type] => 0 )

I’m not getting the last result num_rows => 1 to continue to the following code.

if(mysqli_num_rows($resultado) == 1):
$dados = mysqli_fetch_array($resultado);
mysqli_close($connect);
$_SESSION['logado'] = true;
$_SESSION['id_usuario'] = $dados['id'];
header('Location: home.php');
    else:
       $erros[] = "<li> Usuário e senha não conferem </li>";
           endif;
    endif;
  • You have already debugged your code and passed the result directly to the database to check if it returns something?

  • When registering the user in the database, you encrypt the password twice with the MD5? 'Cause that’s what’s going on in the code.

  • md5 was my mistake. It is only once in code . In BD I can see the result of the correct line

  • After the $result = mysqli_query($connect, $sql); from the last line the [num_rows] => 0

  • at last select replaces * with fields and it already worked. Can anyone explain why ?

No answers

Browser other questions tagged

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