创建自己的类库全局变量失效 急!!!
我在system/application/libraries/文件夹下创建了一个DbObject类在类的上边有如下代码,我用的是PDO
include(APPPATH.'config/database'.EXT);
//use for gengeral DBObject
$dbh = new PDO($db['default']['dbdriver'].':host='.$db['default']['hostname'].';dbname='.$db['default']['database'], $db['default']['username'], $db['default']['password']);
$dbh->exec("set names 'utf8'");
class DbObject
{
我想在DbObject 这个类里边用$dbh这个对像
function get_all()
{
global $dbh;
$sth = $dbh->prepare( 'select * FROM '.'`'.$this->table.'`');
$sth->execute();
if($sth->errorCode() != '00000')
{
print_r($sth->errorInfo());
return false;
}else
{
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
if ($sth->errorCode() != '00000')
{
print_r($sth->errorInfo());
return false;
}else return $rows;
}
}
我又创建一个类Admin
include(APPPATH.'libraries/DbObject'.EXT);
class Admin extends DbObject
{
function __construct()
{
parent::__construct( 'admin','admin_id',array('admin_type_id','admin_name', 'admin_password' ,'admin_belonged_org' ,'admin_enabled' ,'admin_realname' ,'admin_desc','admin_status','builtin','last_login_time'));
}
}
在控制器里这样调用
include(APPPATH.'libraries/Admin'.EXT);
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->library('Admin');
print_r($this->admin->get_all());
$this->load->view('welcome_message');
}
}
上边这样调用$dbh就是个对像,
如果去调最上的include(APPPATH.'libraries/Admin'.EXT);这句的话
$dbh就为空,感觉$this->load->library('Admin');好像没用,高手指点一下,急着用呢 咋没人理我??急呀 你为什么要自己 include 呢?ci 里不需要 include,请严格按照手册来编写代码。 不用include我咋调用其它页面的变量? 看着你的代码,我无语了,你确实用的是CI的框架,可是代码根本没有框架的痕迹,(框架是不需要自己include的),强烈建议看看书册 要得,我先看看 框架是面向对象的
所有内容都 包含在一个叫做CI的对象里。
$this->CI =& get_instance();
页:
[1]