douyiyi5284 2015-01-20 20:46
浏览 98
已采纳

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

im studing php :) In first, sorry for my bad english, I try speak normaly :)

I always start writing some code with database, but all time have problems with extends. Please help me.

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 must including session and dbconnection class

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

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

database.php class where i tried connect to database

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 this class tri select information from my bd if user have a session cookie

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;
        }
    }
}

if this posible, i need that core class includes session and database classes, and thes session class included database and core classes

  • 写回答

1条回答 默认 最新

  • doushua7737 2015-01-20 21:00
    关注

    You're getting this error because you are calling prepare() on the session::db variable, this variable is of type dbconnection and so does not contain a prepare() method, you'd need to change your code to:

    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;
            }
        }
    }
    

    (I have added a second ->db in order to refer to the dbconnection::db variable which is of type PDO It may be worth rethinking the structure of your classes as, as you've discovered, the current structure can easily lead to confusion.

    Here's a suggested structure for these classes:

    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;
            }
        }
    }
    

    Main changes -Removed $db variables everywhere -Capitalised class names -Moved setup tasks, e.g., DbConnection::connect() to constructors

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?