自己写的一个MySQL类

<?php
class Mysql {
private $conn = null; //连接数据库的“管道”
private $sql = ''; //执行的SQL语句
private $conf = null; //配置参数
private $table = ''; //操作的表名
private $error = null; //error信息
private $error_no = ''; //error的原因
private $error_path = ''; //记录错误日志的路径
private $resault = null; //SQL执行结果
private $sql_log = ''; //记录SQL语句的位置
private $error_info = array(
0 => "无法连接数据库",
1 => "连接数据库失败",
2 => "未指定数据库名称",
3 => "选择的数据库不存在",
4 => "选择字符编码失败"
);


public function __construct($conf=null,$table){
$this->conf = $conf;
$this->table = $table;
$this->mysql_conn();
$this->select_db();
$this->select_charset();
}


/**
* 获取数据库连接
**/
private function mysql_conn(){
if(!isset($this->conf['hostname']) || $this->conf['hostname'] == '' || 
  !isset($this->conf['username']) || $this->conf['username'] == '' || 
  !isset($this->conf['password']) || $this->conf['password'] == ''){
$this->error_no = 0;
$this->error = new Exception($this->error_info[$this->error_no]);
$this->echo_error();
}
if(!$this->conn = mysql_connect($this->conf['hostname'],$this->conf['username'],$this->conf['password'])){
$this->error_no = 1;
$this->error = new Exception($this->error_info[$this->error_no]);
$this->echo_error();
}
}


/**
* 选择数据库
**/
private function select_db(){
if(!isset($this->conf['db_name']) || $this->conf['db_name'] == ''){
$this->error_no = 2;
$this->error = new Exception($this->error_info[$this->error_no]);
$this->echo_error();
}
if(!mysql_select_db($this->conf['db_name'])){
$this->error_no = 3;
$this->error = new Exception($this->error_info[$this->error_no]);
$this->echo_error();
}
}


/**
* 设置字符编码
**/
private function select_charset(){
if(!isset($this->conf['charset']) || $this->conf['charset'] == ''){
$this->conf['charset'] = 'utf8';
}
if(!mysql_set_charset($this->conf['charset'])){
$this->error_no = 4;
$this->error = new Exception($this->error_info[$this->error_no]);
$this->echo_error();
}
}


/**
      * 执行查询语句
      **/
public function execute_query($col='',$where='',$join='',$order=''){
$this->add_sql_col($col);
$this->add_sql_where($where);
$this->add_sql_join($join);
$this->add_sql_order($order);
$this->sql .= ";";
return $this->execute_sql($this->sql);
}


/**
* SQL语句添加join部分
**/
public function execute_sql($sql){
$res = null;
if(!$this->resault = mysql_query($sql,$this->conn)){
$this->error = $this->error = new Exception("执行SQL语句失败:".$sql);
$this->echo_error();
}else{
while($row = mysql_fetch_assoc($this->resault)){
$res[] = $row;
}
}
$this->log_sql();
return $res;
}


/**
* SQL语句添加join部分
**/
private function add_sql_join($join){
if((gettype($join) == 'string' || gettype($join) == 'array') && ($join != array() && $join != '')){

}
}


/**
* SQL语句添加order部分
**/
public function add_sql_order($order){
if((gettype($order) == 'string' || gettype($order) == 'array') && ($order != array() && $order != '')){
$sql = $this->sql." ORDER BY ";
switch(gettype($order)){
case 'string' :
$sql .= $order;
break;
case 'array' :
$keys = array_keys($order);
$values = array_values($order);
for($i=0;$i<count($order);$i++){
if(strtoupper($values[$i]) != 'DESC' && strtoupper($values[$i]) != 'ASC'){
$this->error = new Exception("order语句中排序方式出错");
$this->echo_error();
}
$sql .= $keys[$i]." ".strtoupper($values[$i]);
if($i+1 != count($order)){
$sql .= ',';
}
}
break;
}
$this->sql = $sql;
}
}


/**
* 给SQL语句添加where条件
**/
public function add_sql_where($where){
if((gettype($where) == 'string' || gettype($where) == 'array') && ($where != array() && $where != '')){
$sql = $this->sql." WHERE ";
switch (gettype($where)){
case 'string' :
$sql .= $where;
break;
case 'array' :
$keys = array_keys($where);
$values = array_values($where);
$count = count($where);
$type = array("string","integer","double","boolean");
for($i=0;$i<$count;$i++){
if(gettype($keys[$i]) != 'string'){
$this->error = new Exception("SQL中使用了".gettype($keys[$i])."类型参数当where条件等号前");
$this->echo_error();
}
if(!in_array(gettype($values[$i]),$type)){
$this->error = new Exception("SQL中使用了".gettype($values[$i])."类型参数当where条件等号后");
$this->echo_error();
}
$sql .= $keys[$i]."='".$values[$i]."'";
if($i+1 != $count){
$sql .= ',';
}
}
break;
}
$this->sql = $sql;
}
}


/**
* 选择要查询的字段
**/
public function add_sql_col($col){
$sql = "SELECT ";
switch (gettype($col)){
case 'string' :
$col = $col == ''? '*':$col;
$sql .= $col;
break;
case 'array' :
if(count($col) == 0){
$sql .= "*";
}else{
for($i=0;$i<count($col);$i++){
if($i+1 == count($col)){
$sql .= " $col[$i] ";
}else{
$sql .= " $col[$i], ";
}
}
}
break;
case 'object' :
$this->error = new Exception("SQL中使用了".gettype($col)."类型参数当做字段名");
$this->echo_error();
break;
default :
$this->error = new Exception("SQL中使用了".gettype($col)."类型参数当做字段名");
$this->echo_error();
break;
}
$sql .= " FROM ".$this->table;
$this->sql = $sql;
}




/**
* 打印错误信息
**/
private function echo_error(){
if($this->conf['debug']){
echo $this->error->__toString().'<br/>';
}
if($this->conf['log_error']){
$this->log_error();
}
exit(0);
}


/**
* 记录错误日志
**/
private function log_error(){
date_default_timezone_set('Asia/Chongqing');
$this->error_path = $this->conf['log_path'].'/error/'.date('Y-m-d',time());
if(!is_dir($this->error_path)){
$this->mk_folder($this->error_path);
}
$this->error_path .= "/mysql_error.log";
file_put_contents($this->error_path, $this->error->__toString()."\r\n",FILE_APPEND) or die("错误日志无法写入");
}


/**
* SQL语句添加join部分
**/
private function log_sql(){
if($this->conf['log_sql']){
date_default_timezone_set('Asia/Chongqing');
$this->sql_log = $this->conf['log_path'].'/log/'.date('Y-m-d',time());
if(!is_dir($this->sql_log)){
$this->mk_folder($this->sql_log);
}
$this->sql_log .= "/sql.log";
$content = date('Y-m-d H:m:s',time())."---".$this->sql;
file_put_contents($this->sql_log, $content."\r\n",FILE_APPEND) or die("错误日志无法写入");
}
}


/**
* 递归创建目录
**/
private function mk_folder($path){
   if(!is_readable($path)){
    $this->mk_folder( dirname($path) );
    if(!is_file($path)){
        mkdir($path,0777);
    }
   }
    }
}


/*$conf = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'sanyue',
'db_name' => 'xiaoshuo',
'charset' => 'utf8',
'debug' => true,
'log_error' => true,
'log_path' => 'D:/wamp/log/xiaoshuo/',
'log_sql' => true
);
$mysql = new Mysql($conf,'User');
$all = $mysql->execute_query(array(),array(),'',array('username'=>'asc','age'=>'asc'));
print_r($all);*/
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值