简体   繁体   中英

SQL error. SESSION can't have primary key from SELECT

表qn_groups

This is how my table ooks in phpMyAdmin. I have a login script that when the login details match a group's, it should initialize all the $_SESSION variables for groups from the query but after the login script if I try

isset($_SESSION['group_id'])

it returns false. Below is the login function. Note that group_id is an autoincrement primary key

public static function Login($username, $password, $conn)
    {
        if(!isset($conn))
            die("Database connection not established");

        //Try logging in as a user first
        $sql = 'SELECT * FROM qn_users WHERE u_email="'. $username .'" and u_pass="'.md5($password).'" ;';
        $sqlval = mysql_query($sql, $conn);
        if(mysql_num_rows($sqlval) == 1)
        {
            session_start();
            $_SESSION['type'] = "user";
            $_SESSION['email'] = $username;
            $_SESSION['name'] = $sqlval['u_name'];
            $_SESSION['score'] = $sqlval['score'];
            mysql_free_result($sqlval);
            return 1;
        }
        mysql_free_result($sqlval);

        //Now try to log in as group
        $sql = 'SELECT * FROM qn_groups WHERE group_email="'. $username .'" and group_pass="'.md5($password).'" ;';
        $sqlval = mysql_query($sql, $conn);
        if(mysql_num_rows($sqlval) == 1)
        {
            session_start();
            $_SESSION['type'] = "group";
            $_SESSION['email'] = $username;
            $_SESSION['name'] = $sqlval['group_name'];
            $_SESSION['noOfUsers'] = $sqlval['noOfUsers'];
            $_SESSION['group_id'] = $sqlval['group_id'];
            mysql_free_result($sqlval);
            return 1;
        }
        mysql_free_result($sqlval);
        return 0;
    }

$conn is the mysql connection

$conn = @mysql_connect(dbdetails here) or die();

The connection is established. That's evident in the site but after I log in as a group, the group_id doesn't appear to be set. I need the group_id for when the group adds a user but when I ran the sql for that, I found out that there is nothing in $_SESSION['group_id'].

Note: Usage of the mysql is deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.and it advisable to use mysqli.* or PDO along with prepared statements.

All the conditions what you have done it correct. In order to save/display the value that is being displayed from the DB you have to use any one of the methods available in mysql.

It is mandatory to use any of the below methods.

mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both

mysql_fetch_assoc() - Fetch a result row as an associative array

mysql_fetch_object() - Fetch a result row as an object

mysql_data_seek() - Move internal result pointer

mysql_fetch_lengths() - Get the length of each output in a result

mysql_result() - Get result data

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