功能:操作数据库 模版引擎smarty
分析,应该具备什么功能?
a:具体配置文件 ,配置文件的读取功能
b:数据库处理类
c:目录结构
先建一个includes文件放入,配置文件(config.php),配置类(conf.class.php),mysql数据库处理类(mysql.class.php),网站初始化文件(init.php)
config.php 文件:
//网站的配置文件 $_cfg=array(); $_cfg['host']='localhost'; $_cfg['user']='root'; $_cfg['pwd']='root'; $_cfg['db']='xxzdb'; $_cfg['char']='utf8'; //$_cfg['']=''; //$_cfg['']='';
conf.class.php
//配置文件的读取类 //作用:读取config.php 并能返回某个配置选项的值 class Conf{ private static $ins = false; private $info = array(); final protected function __construct(){ require(ROOT .'includes/config.php'); $this->info = $_cfg; } //单例 public static function getIns(){ if(self::$ins === false){ self::$ins = new self(); } return self::$ins; } //读取配置文件的信息 public function __get($key){ if(array_key_exists($key,$this->info)){ return $this->info[$key]; } else { return null; } } public function __set($key,$value){ $this->info[$key] = $value; } //测试方法 public function printc(){ print_r($this->info); } } //调用 /* $conf = Conf::getIns(); $conf->template_dir='d:/www'; echo $conf->printc(); */
mysql.class.php
//require('conf.class.php'); //数据库的处理类 //抽象类 没有方法体 abstract class abs_db{ abstract protected function connect(); abstract protected function select_db($dbname=''); abstract protected function setChar(); abstract protected function query($sql); abstract protected function getAll($sql); abstract protected function getRow($sql); abstract protected function getOne($sql); abstract protected function error(); } class Mysql extends abs_db{ private static $ins = false; private $conn = false; private $conf = false; protected function __construct(){ $this->conf = Conf::getIns(); $this->connect(); $this->select_db(); $this->setChar(); } public static function getIns(){ if(self::$ins === false){ self::$ins= new self(); } return self::$ins; } //创建连接 protected function connect(){ $this->conn = mysql_connect($this->conf->host,$this->conf->user,$this->conf->pwd); if(!$this->conn){ $err = new Exception('连接失败'); throw $err; } } //选择数据库 protected function select_db($dbname=''){ if($dbname == ''){ $sql='use '.$this->conf->db; $this->query($sql); } } //设置字符集 protected function setChar(){ $sql='set names '.$this->conf->char; $this->query($sql); } //执行sql语句 public function query($sql){ return mysql_query($sql,$this->conn); } //取回所有行 public function getAll($sql){ $rs = $this->query($sql); $list=array(); while($row = mysql_fetch_assoc($rs)){ $list[] = $row; } return $list; } //取回一行 public function getRow($sql){ $rs = $this->query($sql); return mysql_fetch_assoc($rs); } public function getOne($sql){ } public function error(){ print_r(mysql_error($this->conn)); } //关闭资源 public function __destruct(){ } } /* $db = Mysql::getIns(); print_r($db); $sql="select * from art limit 5"; print_r($db->getAll($sql)); */
init.php
//网站的初始化文件 /** 魔术方法 __FILE__ 当前绝对路径 文件作用: 负责当前网站的根目录 引入所以页面都需要的文件 */ //ROOT 代表网站的跟路径 define('ROOT',str_replace('\\','/',str_replace('includes\init.php','',__FILE__))); require(ROOT .'includes/conf.class.php'); require(ROOT .'includes/mysql.class.php'); //echo ROOT;