<?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 getOne($sql); //取一个值
abstract protected function error(); //报错
}
class mysql extends abs_db{
private static $ins=false;
private $conn=false;
private $conf=false;
final protected function __construct(){
$this->conf=conf::getIns(); //获取conf类实例
$this->connect();
$this->select_db();
$this->setChar();
}
final protected function __clone(){ //防止克隆
}
public static function getIns(){
if(self::$ins===false){
self::$ins=new self();
}
return self::$ins;
}
protected function connect(){ //连接数据库方法
//$this-conf是conf类的一个实例,conf类又通过__get方法获取
$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;
return $this->query($sql);
}
public function query($sql){ //查询
if($this->conf->bug){
$this->log($sql);
}
$res=mysql_query($sql,$this->conn);
if(!$res){
$this->log($this->error());
}
return $res;
}
//自动调用添加用户方法
//implode连接字符串到数组
public function autoExecute($arr,$table,$model='insert', $where='where 1 limit 1'){
if(!is_array($arr)){ //判断是否为数组
return false;
}
if($model=='update'){ //如果是更新模式
$sql="$model $table set ";
foreach($arr as $key=>$val){
$sql.="$key='$val' ,";
}
$sql=rtrim($sql,','); //去,号
$sql.=$where;
return $this->query($sql);
}
$sql='insert into '.$table.'( '.implode(',',array_keys($arr)). ')';
$sql.='values ( \'';
$sql.=implode("','",array_values($arr)).'\')';
return $this->query($sql); // var_dump($res);
}
public function getAll($sql){ //返回所有行
$rs=$this->query($sql);
$list=array();
while(!!$row = mysql_fetch_assoc($rs)) { //循环取数据
$list[]=$row; //赋值给list数组
}
// var_dump($list);
mysql_free_result($rs); //释放资源
return $list; //返回的是二维数组
}
public function log($sql){
$fileName=ROOT.'data/sql.log'; //日志文件目录
$maxsize=1024*1024;
if(!file_exists($fileName)){ //文件不存在则与写入方式创建
$fp=fopen($fileName,'w');
}else{
if(filesize($fileName)>=$maxsize){ //如果大于1M则清空
$fp=fopen($fileName,'w');
}else{
$fp=fopen($fileName,'a'); // 与追加模式打开
}
}
fwrite($fp,$sql."\r\n");
fclose($fp);
}
public function getRow($sql){ //返回一行
$rs=$this->query($sql);
return mysql_fetch_assoc($rs); //返回一行数据
}
public function getOne($sql){ //取一个值
$rs=$this->query($sql);
$row=mysql_fetch_row($rs);
return $row[0];
}
public function affected_row(){ //取得影响的行数
return mysql_affected_rows($this->conn);
}
public function increment_id(){ //取得最新自增长的id
return mysql_insert_id($this->conn);
}
public function error(){ //报错
return mysql_error('连接失败'.$this->conn);
}
public function close(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>
转载于:https://my.oschina.net/u/932154/blog/100172