Select that returns JSON

Asked

Viewed 78 times

2

Hello! I have the following PHP code:

login.php

<?php 
    $msg;
    if (isset($_POST['login']) && isset($_POST['senha'])) {
        $login = str_replace(" ", "", $_POST['login']);
        $senha = str_replace(" ", "", $_POST['senha']);

        if (strlen($login)<4 || strlen($login)>50) {
            $msg = array('status' => 0, 'msg' => "Login deve ter entre 4 a 50 caracteres!" );
        }elseif (strlen($senha)<8 || strlen($senha)>20) {
            $msg = array('status' => 0, 'msg'=> "Senha deve ter entre 8 a 20 caracteres");
        }else{
            require_once("logar.php");
        }
    }else{
        $msg = array('status' => 0, 'msg'=>"Informe o Usuario e Senha!");
    }
    echo json_encode($msg);
 ?>

log in.php

<?php 
    $login = $_POST['login'];
    $senha = $_POST['senha'];

    include "conexao.php";

    $sql = "SELECT * FROM usuario WHERE usuemail='$login' AND ususenha = MD5('$senha')";
    $resultado = mysqli_fetch_assoc($conexao->query($sql));
    if ($resultado) {
        
        $msg = array('status' =>1 , 'user' =>$resultado);

    }else{
        $msg = array('status' =>2 , 'msg' => "Usuario informado não existe!" );
    }
    

 ?>

in the right bank groove, the more I’m testing it in the ADVENCED REST CLIENT and only returns that the user does not exist. Someone can help me

  • Where is the json_encode() in the second code?

  • login.php already does this.. It accesses the login.php and oega from there.

1 answer

0

You are checking whether a variable of type array is true :/, in fact you have to check if it is empty or if it has returned a user.

$row =mysqli_fetch_assoc($result);
if($row > 0){
   //....
}

or

if($row == 1){
  //....
}
  • 1

    thanks. solved here... the error was in the password field of the bank was varchar(20), as the password is in md5 did not save all characters, saved only 20 and never worked the password... Thanks more for the willingness to help..

Browser other questions tagged

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