致命错误:调用未定义的方法dbconnection :: prepare()

时间:2015-01-20 20:46:16

标签: php mysql oop pdo

我正在学习php :) 首先,对不起我的英语不好,我试着说法:)

我总是开始用数据库编写一些代码,但是所有时间都有扩展问题。请帮帮我。

的index.php

define('CWM', TRUE);
define('DS', DIRECTORY_SEPARATOR);
define('PATH', dirname(__FILE__) . DS);
define('LINK', dirname($_SERVER['SCRIPT_NAME']));

require_once 'classes' . DS . 'database.php';
require_once 'classes' . DS . 'session.php';
require_once 'classes' . DS . 'core.php';
$core = new core;

core.php必须包含session和dbconnection类

if(!defined('CWM')) die('script access error');
class core extends session{
    protected $db;

    function __construct(){
        $this->db = new dbconnection();
        parent::session();
    }
}

database.php类我尝试连接数据库

class dbconnection{
protected $db;
protected $dbinfo = array();

public function connect(){
        if(file_exists(PATH . 'classes' . DS . 'config.php')){
            $this->dbinfo = require_once PATH . 'classes' . DS . 'config.php';

            try{
                $this->db = new PDO('mysql:host=' . $this->dbinfo['hostname'] . ';dbname='. $this->dbinfo['dbname'], $this->dbinfo['username'], $this->dbinfo['password'], array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
                return $this->db;
            }catch(PDOException $e){
                die($e->getMessage());
            }
        }else{
            trigger_error('undefined config.php', E_USER_ERROR);
        }
    }

    function __destruct(){
        $this->db = NULL;
    }
}

session.php这个类如果用户有会话cookie,则从我的bd中选择信息

if(!defined('CWM')) die('script access error');
class session extends dbconnection{
    protected $db;
    protected $member = array();

    function __construct(){
        parent::connect();
        $this->session;
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

如果这个可行,我需要核心类包括会话和数据库类,并且会话类包括数据库和核心类

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为您在prepare()变量上调用session::db,此变量的类型为dbconnection,因此不包含prepare() 1}}方法,您需要将代码更改为:

if(!defined('CWM')) die('script access error');
class session extends dbconnection{
    protected $db;
    protected $member = array();

    function __construct(){
        parent::connect();
        $this->session;
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

(我添加了第二个->db以便引用PDO类型的dbconnection::db变量 可能值得重新思考课程的结构,正如您所发现的那样,当前的结构很容易引起混淆。

以下是这些课程的建议结构:

core.php中

if(!defined('CWM')) die('script access error');
class Core extends Session {

    function __construct(){
        parent::__construct();
        $this->session();
    }
}

DbConnection.php

class DbConnection{
    protected $db;
    protected $dbinfo = array();

    public function __construct() {
        $this->connect();
    }    

    public function connect(){
        if(file_exists(PATH . 'classes' . DS . 'config.php')){
            $this->dbinfo = require_once PATH . 'classes' . DS . 'config.php';

            try{
                $this->db = new PDO('mysql:host=' . $this->dbinfo['hostname'] . ';dbname='. $this->dbinfo['dbname'], $this->dbinfo['username'], $this->dbinfo['password'], array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
                return $this->db;
            }catch(PDOException $e){
               die($e->getMessage());
           }
        }else{
            trigger_error('undefined config.php', E_USER_ERROR);
        }
    }

    function __destruct(){
        $this->db = NULL;
    }
}

session.php文件

if(!defined('CWM')) die('script access error');
class Session extends DbConnection {

    protected $member = array();

    function __construct(){
        parent::__construct();
    }

    protected function session(){
        $_COOKIE['session'] = 5;
        if(!empty($_COOKIE['session'])){
            $this->member = $this->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
            var_dump($this->member);
        }else{
            $this->member = false;
        }
    }
}

主要变化 - 到处删除$ db变量 - 资本化的班级名称 - 移动设置任务,例如,DbConnection :: connect()到构造函数