duanmen1887 2016-12-30 14:47 采纳率: 0%
浏览 24

PHP PDO:“不在对象上下文中使用$ this [...]”

I am receiving the following error while using the php code below:

[error] 7096#0: *410 FastCGI sent in stderr: "PHP message: PHP Fatal error: Using $this when not in object context in /var/www/html_offline/core.php on line 60" while reading response header from upstream, client: [...]

I've already tried to solve this issue but couldn't find a solution.

<?php

class CORE {

    private $db_sqlite;
    private $db_mysql;

    function __construct(){
        $this->db_mysql = $this->get_MySQL();
        $this->db_sqlite = $this->get_SQLite();
    }

    //
    //  MYSQL ADAPTER
    //
    function get_MySQL($db = null) {
        if ($db == null) {
            try {
                $db = new PDO("mysql:host=ip_address:port;dbname=database;charset=utf8","user","password", array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
            } catch(PDOException $ex) {
                return $db;
            }
        }
        return $db;
    }

    //
    //  SQLITE ADAPTER
    //
    function get_SQLite($db = null) {
        if ($db == null) {
            try {
                $db = new PDO("sqlite://var/www/html_offline/ip_throttling.db", null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
                $db->query("CREATE TABLE IF NOT EXISTS storage (IP VARCHAR(255), TIME VARCHAR(255), ID VARCHAR(255))");
            } catch(PDOException $ex) {
                return $db;
            }
        }
        return $db;
    }

    //
    //  IP QUERY RESTRICTION
    //
    function isThrottled($ip, $id) {
        //  SELECT stmt
        $stmt = $this->db_sqlite->prepare('SELECT TIME FROM storage WHERE IP = :ip AND ID = :id');
        $bind = array(
            ':ip' => $ip,
            ':id' => $id
        );
        $stmt->execute($bind);
        if ($stmt->rowCount() > 0) {
            $fetch = $stmt->fetch(PDO::FETCH_ASSOC);
            if ($fetch['time'] <= strtotime('-30 seconds')) {
                return true;
            }
            //  DELETE stmt
            $stmt = $this->db_sqlite->prepare('DELETE FROM storage WHERE IP = :ip AND ID = :id');
            $stmt->execute($bind);
            return false;
        } else {
            return false;
        }
    }

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据