I can not assign a database value to a php variable

2

Hello everyone, I'm new to php and I'm having a hard time getting a user table (id, login, password, level) according to the level of the user should go to a different panel, the login part is ok, only the level value I can not assign to a variable to make a comparison and direct the user to their panel

follow 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>";
}
    
asked by anonymous 23.08.2016 / 05:55

1 answer

1

You did not show how you are trying to assign or if something is wrong, basically this 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'];
    
23.08.2016 / 16:26