Error while trying to recover Mysql data with PHP

Asked

Viewed 38 times

1

I’m making a web page that contains login for an administrative area, but where I’m creating and testing (on a local server) is working normally. When I copy the project and put it on my laptop to test and present the project, the same code shows error.

Where I am creating the page and testing is Linux, in my notebook 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 that presents in the 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

  • Before line 28, right after mysqli_query, place die(mysqli_error($con)) and see if any error message appears on the screen.

  • Appears Champ 'username' inconnu dans Where clause

  • The column username exists in the table tb_colaboradores?

  • ssanosaaa that vacilo was with user in the table and was calling username. thank you very much [solved]

1 answer

0

Try checking returned lines and if return more than 0 Then you take the data(s) (s) returned(s):

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);

Plus, I added a MYSQLI_ASSOC to return the values in array

  • what saddens me most is that the same routine works where the site is being built and tested which in case is my PC.

Browser other questions tagged

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