简体   繁体   English

SQL错误。 SESSION不能具有SELECT的主键

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

表qn_groups

This is how my table ooks in phpMyAdmin. 这就是我的表在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 我有一个登录脚本,当登录详细信息与组的匹配时,它将初始化查询中组的所有$ _SESSION变量,但是如果尝试登录脚本,

isset($_SESSION['group_id'])

it returns false. 它返回false。 Below is the login function. 下面是登录功能。 Note that group_id is an autoincrement primary key 请注意,group_id是自动递增的主键

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连接

$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. 这在站点上很明显,但是当我作为一个组登录后,似乎没有设置group_id。 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']. 当组添加用户时,我需要使用group_id,但是当我为它运行sql时,我发现$ _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. 注意: mysql的用法在PHP 5.5.0中已被弃用,在PHP 7.0.0中已被删除,建议将mysqli。*或PDO与准备好的语句一起使用。

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. 为了保存/显示从数据库中显示的值,您必须使用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_array() -提取结果行为关联数组,数字数组或两者

mysql_fetch_assoc() - Fetch a result row as an associative array mysql_fetch_assoc() -获取结果行作为关联数组

mysql_fetch_object() - Fetch a result row as an object mysql_fetch_object() -获取结果行作为对象

mysql_data_seek() - Move internal result pointer mysql_data_seek() -移动内部结果指针

mysql_fetch_lengths() - Get the length of each output in a result mysql_fetch_lengths() -获取结果中每个输出的长度

mysql_result() - Get result data mysql_result() -获取结果数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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