php - MySQL 的 session 处理程序困难

标签 php mysql

正在观看 this online tutorial关于 MYSQL 的 session 处理程序,并对这部分感到非常困惑:

table_XXX == 表 XXX; col_XXX == 第 XXX 列; sid == session ID

读取方法:

public function read($session_id)
    {
        $this->db->exec('SET TRANSACTION ISOLATION LEVEL READ COMMITTED');
        $this->db->beginTransaction();
        /**
         * the data is selected and no other ppl can interfere
         * the writing process until COMMIT is reached
         */
        $sql = "SELECT $this->col_expiry, $this->col_data
                FROM $this->table_sess
                WHERE $this->col_sid = :sid FOR UPDATE";
        $selectStmt = $this->db->prepare($sql);
        $selectStmt->bindParam(':sid', $session_id);
        $selectStmt->execute();
        $results = $selectStmt->fetch(\PDO::FETCH_ASSOC);
        if ($results) {
            if ($results[$this->col_expiry] < time()) {
                // return empty if data out of date
                return '';
            }
            return $results[$this->col_data];
        }

        return $this->initializeRecord($selectStmt);
    }

protected 方法:

protected function initializeRecord(\PDOStatement $selectStmt)
    {
        try {
            $sql = "INSERT INTO $this->table_sess 
                    ($this->col_sid, $this->col_expiry, $this->col_data)
                    VALUES (:sid, :expiry, :data)";
            $insertStmt = $this->db->prepare($sql);
            $insertStmt->bindParam(':sid', $session_id);
            $insertStmt->bindParam(':expiry', $this->expiry); // expiry is defined
            $insertStmt->bindValue(':data', '');
            $insertStmt->execute();
            return '';
        } catch(\PDOException $e) {
            $this->db->rollBack();
            throw $e;
        }
    }

写法:

public function write($session_id, $data)
    {
        try {
            $sql = "INSERT INTO $this->table_sess ($this->col_sid,
                    $this->col_expiry, $this->col_data)
                    VALUES (:sid, :expiry, :data)
                    ON DUPLICATE KEY UPDATE
                    $this->col_expiry = :expiry,
                    $this->col_data = :data";
            $stmt = $this->db->prepare($sql);
            $stmt->bindParam(':expiry', $this->expiry, \PDO::PARAM_INT);
            $stmt->bindParam(':data', $data);
            $stmt->bindParam(':sid', $session_id);
            $stmt->execute();
            return true;
        } catch (\PDOException $e) {
            if ($this->db->inTransaction()) {
                $this->db->rollback();
            }
            throw $e;
        }
    }

在“Protected method”的第 8 行中,有一个 $session_id,显然没有 $session_id 传递给 protected 方法,所以那一行的 bindParam() 只是没有绑定(bind)任何东西? 所以 initializeRecord() 只是启动了一个有过期时间但没有别的的行?然后调用write方法后插入sid和data?

最佳答案

这在创建 SQL 语句时使用 WHERE $this->col_sid = :sid 等做了很多字符串构造技巧。

在对它们运行 ->execute() 之前,您可以尝试回显或转储这些 SQL 语句以查看它们包含的内容。这将帮助您排除故障。

很明显,您的 protected 方法缺少 $session_id。您可以在那里使用 $this->sid 的值吗?

关于php - MySQL 的 session 处理程序困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215760/

相关文章:

php - Codeigniter:获取页面上的所有查询

php - yum 在 centos 上安装 php-pear*

php - 将 mysql 结果与 php 类一起使用的最有效方法

c# - SQL Server 的 IP 地址连接字符串

php - 在 PHP 4 中获取本周星期一的日期

php - 获取值 mysql 计数并保存到表中

php - 调用成员函数 count()

php - 在 MySQL/PHP 脚本中使用 php time() 而不是 now()

php - 根据订单栏显示产品订单

php - 如何获得给定数据库的另一个根