简体   繁体   中英

How can I get the enum value of SQL database in PHP

So I'm saving user's ranks into a SQL database as enumerators. I then want to try and check their rank and compare it to a string. If the rank is equal to the string then I want to allow them access. Here is my code so far (Which doesn't work):

    $email = stripslashes($email);
    $password = stripslashes($password);
    $email = mysql_real_escape_string($email);
    $password = mysql_real_escape_string($password);
    $sql = "SELECT RANK FROM $table WHERE EMAIL = '$email' and PASSWORD = '$password'";
    $result = mysql_query($sql);

    $count = mysql_num_rows($result);

    if($count == 1){
        if($result == "OWNER") {
            header("location: panel.php");
        } else {
            $message = "Permission denied.";
        }
    } else {
        $message = "Incorrect email or password.";
    }

IMPORTANT NOTICE!

mysql_* functions are deprecated since PHP 5.5.0. It's highly recomended to use MySQLi or PDO instead.

Answering your question, you have to fetch the query result or use mysql_result()

Query

$sql = "SELECT RANK
        FROM $table
        WHERE EMAIL = '$email' and PASSWORD = '$password'
        LIMIT 1";
$result = mysql_query($sql);

Fetch the result:

$rank = mysql_fetch_array($result);
$rank = $rank['RANK'];

Or use mysql_result() instead:

$rank = mysql_result($result);

you have to mysql_fetch_assoc the $result var before you can read the table row. Also I would recmmend you to use mysqli functions. Here, try this instead of yours (without mysqli, and not tested by myself):

$email = mysql_real_escape_string(stripslashes($email));
$password = mysql_real_escape_string(stripslashes($password));
$sql = "SELECT `RANK` FROM ".$table." WHERE `EMAIL` = '".$email."' and `PASSWORD` = '".$password."'";
$result = mysql_query($sql);

$count = mysql_num_rows($result);

$result = mysql_fetch_assoc($result);

if($count == 1){
    if($result == "OWNER") {
        header("location: panel.php");
    } else {
        $message = "Permission denied.";
    }
} else {
    $message = "Incorrect email or password.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM