|
我在system/application/libraries/文件夹下创建了一个DbObject类
在类的上边有如下代码,我用的是PDO
PHP复制代码 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这个对像
PHP复制代码 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
PHP复制代码 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'));
}
} 复制代码
在控制器里这样调用
PHP复制代码 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');好像没用,高手指点一下,急着用呢 |
|