我正在学习 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 如果用户有 session 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;
}
}
}
如果可能的话,我需要核心类包括 session 和数据库类,并且这些 session 类包括数据库和核心类
最佳答案
您收到此错误是因为您在 session::db
变量上调用 prepare()
,该变量的类型为 dbconnection
,因此不包含 prepare()
方法,您需要将代码更改为:
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
变量
以下是这些类的建议结构:
核心.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())移至构造函数
关于php - fatal error :调用未定义的方法 dbconnection::prepare(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28054827/