Error trying to retrieve data from MySQL with PHP

1

I'm making a webpage that contains login for an administrative area, but where I'm creating and doing the tests (on a local server) is working normally. When I copy the project and put it in my notebook to test and present the project, the same code has an error.

Where I am creating the page and testing is Linux, in my notebook it is Windows.

The MySQL routine is this:

if (isset($_POST["f_logar"])) {

    $user=$_POST["f_user"];
    $senha=$_POST["f_senha"];

    $sql="SELECT * FROM tb_colaboradores WHERE username='$user' AND senha='$senha'";
    $res=mysqli_query($con,$sql);
    $ret=mysqli_fetch_array($res);

    //MySQL
    //pesq.user, se existir
    //obter user e senha
    //comparar. senha

    if($ret == 0){

        echo "<p id='lgErro'>Login inválido</p>";

    }else{
        $chave1="abcdfghijklmnopqrstuvwyz";
        $chave2="ABCDFGHIJKLMNOPQRSTUVWYZ";
        $chave3="0123456789";
        $chave=str_shuffle($chave1.$chave2.$chave3);
        $tam=strlen($chave);
        $num="";
        $qtde=rand(20,50);

        for ($i=0;$i<$qtde;$i++) { 

            $pos=rand(0,$tam);
            $num.=substr($chave,$pos,1);
        }

        session_start();
        $_SESSION['numlogin']=$num;
        $_SESSION['username']=$user;
        $_SESSION['acesso']=$ret['acesso']; //0=restrito / 1=total

        header("Location:gerenciamento.php?num=$num");
    }
}

mysqli_close($con);

The error you have in your notebook is this:

  

Warnig: mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given in C: \ wamp \ www \ site \ login.php on line 28

    
asked by anonymous 21.05.2017 / 00:15

1 answer

0

Try to check the returned rows and return more than 0 there you get the returned data:

if (isset($_POST["f_logar"])) {

    $user=$_POST["f_user"];
    $senha=$_POST["f_senha"];

    $sql="SELECT * FROM tb_colaboradores WHERE username='$user' AND senha='$senha'";

    $res = mysqli_query($sql);

    //MySQL
    //pesq.user, se existir
    //obter user e senha
    //comparar. senha

    if(mysqli_num_rows($res) <= 0){

        echo "<p id='lgErro'>Login inválido</p>";

    }else{

        $ret=mysqli_fetch_array($res, MYSQLI_ASSOC);

        $chave1="abcdfghijklmnopqrstuvwyz";
        $chave2="ABCDFGHIJKLMNOPQRSTUVWYZ";
        $chave3="0123456789";
        $chave=str_shuffle($chave1.$chave2.$chave3);
        $tam=strlen($chave);
        $num="";
        $qtde=rand(20,50);

        for ($i=0;$i<$qtde;$i++) { 

            $pos=rand(0,$tam);
            $num.=substr($chave,$pos,1);
        }

        session_start();
        $_SESSION['numlogin']=$num;
        $_SESSION['username']=$user;
        $_SESSION['acesso']=$ret['acesso']; //0=restrito / 1=total

        header("Location:gerenciamento.php?num=$num");
    }
}

mysqli_close($con);

In addition, I've added a MYSQLI_ASSOC to return the values in array

    
21.05.2017 / 00:44