duan7664 2016-01-13 15:25
浏览 70
已采纳

实例化变量=新对象; [关闭]

I'm starting doing php OO (I know javaOO) but I wish someone could help me instantiate a variable so that I can call its methods.

A couple of hours ago I read an article and I would do it. How you can pass object session from PHP to MySQL.

I put all class from GitHub end.

I'm using this tutorial: http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/

Full 2 class: https://github.com/plagodzinski/dbsession/tree/master/include

I'm trying instanciate with :

$ObjSession = new Session; // this line get a error and not work 

What I'm wrong ?

Updated 13/01/2016 16;47

This is my error:

Warning Call to a member function prepare() on a non-object

Class Session:

class Session
{
    private $db;

    public function __construct() {

        // instantiate the new database object
        $this->db = new Database();

        // set handler to override session
        session_set_save_handler(
            array($this, "_open"),
            array($this, "_close"),
            array($this, "_read"),
            array($this, "_write"),
            array($this, "_destroy"),
            array($this, "_gc")
        );

        // start the session
        session_start();
    }

    // check if the database connection is up
    public function _open() {
        if ($this->db) {
            return true;
        } 

        return false;
    }

    // close the database connection
    public function _close() {
        if ($this->db->close()) {
            return true;
        }

        return false;
    }

    // read session values
    public function _read($id) {
        $this->db->query('SELECT data FROM sessions WHERE id = :id');

        if ($this->db->execute(array($id))) {
            $row = $this->db->single();
            return $row->data;
        } else {
            return '';
        }
    }

    // write session values
    public function _write($id, $data) {
        $access = time();
        $this->db->query('REPLACE INTO sessions VALUES (:id, :access, :data)');

        if ($this->db->execute(array($id, $access, $data))) {
            return true;
        }

        return false;
    }

    // destroy session
    public function _destroy($id) {
        $this->db->query('DELETE FROM sessions WHERE id = :id');
        if ($this->db->execute(array($id))) {
            return true;
        }

        return false;
    }

    // garbage collection
    public function _gc($max) {
        $old = time() - $max;

        $this->db->query('DELETE * FROM sessions WHERE access < :old');

        if ($this->db->execute(array($old))) {
            return true;
        }

        return false;
    }
}

Class Database:

define("DB_HOST", "");
define("DB_NAME", "");
define("DB_USER", "");
define("DB_PASS", "");

class Database
{
    private $host = DB_HOST;
    private $user = DB_USER;
    private $pass = DB_PASS;
    private $dbname = DB_NAME;
    private $dbh;
    private $error;
    private $stmt;
    public function __construct() {
        // set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // set OPTIONS
        $options = array(
                PDO::ATTR_PERSISTENT => true,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        );
        // create a new PDO instance or catch any errors
        try {
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    // prepare statement
    public function query($query) {
        $this->stmt = $this->dbh->prepare($query);
    }

    // bind values and execute statement
    public function execute(array $params = null) {
        $this->stmt->execute($params);
    }

    // fetch single row result
    public function single() {
        try {
            return $this->stmt->fetch(PDO::FETCH_OBJ);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    // fetch all results
    public function resultset() {
        try {
            return $this->stmt->fetchAll(PDO::FETCH_OBJ);
        } catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    // fetch output parameter from stored procedure
    public function outParam($paramName, $paramAsName) {
        $this->stmt->closeCursor();
        return $this->dbh->query('SELECT ' . $paramName . ' AS ' . $paramAsName)->fetch(PDO::FETCH_OBJ);
    }

    // get affected rows count
    public function rowCount() {
        return $this->stmt->rowCount();
    }

    // get the id of the last inserted row
    public function lastInsertId() {
        return $this->dbh->lastInsertId();
    }

    // begin transaction
    public function beginTransaction() {
        return $this->dbh->beginTransaction();
    }

    // end transaction
    public function endTransaction() {
        return $this->dbh->commit();
    }

    // cancel transaction
    public function cancelTransaction() {
        return $this->dbh->rollBack();
    }

    // debug dump parameters
    public function debugDumpParams() {
        return $this->stmt->debugDumpParams();
    }

    // close connection
    public function close() {
        $this->dbh = null;
    }
}
  • 写回答

1条回答 默认 最新

  • dongyun234854 2016-01-14 00:03
    关注

    Sorry for the delay, I amused myself doing other things:

    Solution :

    I get errors in the same file I do not know very well (best import)

    include('database.class.php');
    include('session.class.php');
    

    Instantiate

    $session = new Session();
    

    Add query / execute (Database)

    PDOException $e
    

    Important all arrays need a token(:) to recognize ":variable" =>$variable

    $this->db->execute(array($id)) to $this->db->execute(array(":id" =>$id))

    After all steps you can save session to mysql

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码