I cannot assign a database value to a php variable

Asked

Viewed 618 times

2

Hello guys I’m beginner in php and I’m having a difficulty have a table of users (id, login, password, level) according to the level of the user should go to a different panel, the login part this ok, only the level value cannot assign to a variable to make a comparison and direct the user to its due panel

follows the code:

<?php

$usuario = $_POST['user'];
$senha = $_POST['password'];
$sql = mysql_query("SELECT * FROM db_usuarios WHERE login = '$usuario' and senha = '$senha'") or die (mysql_error());
$row = mysql_num_rows($sql) or die (mysql_error());
if($row == 1){
    $nivel = 0;
    $query = mysql_query("SELECT * FROM db_usuarios WHERE nivel = '$nivel'");

    echo $nivel; 
    session_start();
    $_SESSION['login'] = $_POST['user'];
    $_SESSION['senha'] = $_POST['password'];
    echo "<center>Login efetuado com Sucesso!!!</center> ";
    //echo "<script>loginsuccessfully()</script>";
    switch($nivel){
        case 1:
            echo "<script>diretor()</script>";
        break;
    }

}else{

    echo "<center>Login ou senha invalidos aguarde um instante para tentar novamente!</center>";
    echo "<script>loginfailed()</script>";
}
  • 2

    its $nivel = 0; is always equal to 0

  • Good morning Thanks for proposing to help I assign zero I see if in the code below it would change value, if I change manually the value of $nivel = 1; it runs the script straight and redirects... But this value I already have in the bank and I can not recover it and assign to $nivel understood I found some things on the web but they did not work, maybe I have implemented wrong

1 answer

1


You have not shown how you are trying to assign or if some error is happening, basically it should be done:

$sql = mysql_query("SELECT * FROM db_usuarios WHERE login = '$usuario' and senha = '$senha'") or die (mysql_error());
$array = mysql_fetch_assoc($sql);

$nivel = $array['nivel'];
  • Hello to Reji Thanks for the reply as soon as I get home I will try to implement

  • Reji, muito Thanks this implementation worked out (y)

  • Ball show. ;)

  • You should mark the answer as accepted by putting a certain green in it :)

  • Hello Amadeu Antunes, scored.

Browser other questions tagged

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