下篇的文章已经更新咯,使用pdo安全性更高,兼容性更强
基于mysqli写的sql封装语句,可支持预处理和事务,可以输出最后执行的sql。
所有的条件以数组传递即为预处理语句,否则为原始语句执行传递字符串
调用方法如下:
$mysqlObj = new mysqliModel();
添加=》
$data['cat_name'] = '测试1111';
$data['spec'] = MD5('描述');
$data['cname'] = '执行三次?';
$data['cat_name'] = '修改测试2';
$result = $mysqliObj->add("category",$data);//第一个参数为表名,第二个参数为处理的数组,若post的数组都是就是表单数据即可不用传递,若部分需要进行加密等操作,只需要将键值写入数组并赋值处理后的数据
修改=>$result = $mysqliObj->save("category",$id,$data);//和添加方法使用一致,只是传递的id为主键值
update是基于add和save方法,可以在添加和修改时都使用$result = $mysqliObj->update("category",$data);
删除=>$result = $mysqliObj->del("category",$id);//主键id删除
$result = $mysqliObj->dels("category",$ids);//根据主键批量删除,返回删除总数。ids可以是一个数组,也可以是逗号分隔的字符串
$result = $mysqliObj->delc("category",$conditon);//根据条件删除
查找=>$result = $mysqObj->find("category",$id);//根据主键查询单条记录
$result = $mysqlObj->select("category");//查询表中所有值
当需要根据一定条件和排序等等限制条件时可以使用连贯操作如$result = $mysqlIbj->field('id')->where("id= 1")->order("id asc,cid desc")->limit(1,2)->select('category);\
where()中可传递数组,即为预处理
输出sql语句:echo $mysqlObj->getsql();或者echo $mysqlObj->_sql();
还有很多的的方法比如分页等等,这里就不详述了,可以自行查看代码
<?php
header("content-type:text/html;charset=utf-8");/**
* autor:sujianbin
* 2016-09-01
* 采用mysqli对象方式连接
* MYSQL中只有INNODB和BDB类型的数据表才能支持事务处理!其他的类型是不支持的!(alter table tablename ENGINE = InnoDB)
* 支持预处理技术,主要在连接和编译过程精简,还可以SQL防止注入,快速执行
*/
class mysqliModel{
private $mysqli = '';//当前mysqli对象
private $field = '';//需要查询的字段
private $where = '';//查询条件
private $order = '';//排序规则
private $limit = '';//查询数量
private $pri = '';//主键
private $table = '';//表名
private $params = array();//预处理数据
private static $host = 'localhost';//主机名或ip地址
private static $db_user = 'root';//用户名
private static $db_pass = 'root';//密码
private static $db_name = 'mymoban';//数据库名称
private $db_press = 'su_';//数据库表前缀
private $db_log = false;//是否开启日志
private $auto_commit = TRUE;//是否开启自动提交,不适用于查询操作,默认自动提交,如若关闭操作时请先调用方法关闭
private $debug = 1;//是否开启调试模式,开启后出现错误查询会出现自动打印出sql语句
/**
* 构造函数
*/
public function __construct($field='',$where='',$order='',$limit='') {
$this->field = $field;
$this->where = $where;
$this->order = $order;
$this->limit = $limit;
$this->mysqli = new mysqli(self::$host,self::$db_user,self::$db_pass,self::$db_name);
if($this->mysqli->connect_error){
echo "Failed to connect to MySQL: " . $this->mysqli->connect_error;exit;
}else{
$this->mysqli->query("SET names UTF8");
session_start();
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set("PRC");
}
}
/**
* __get()方法用来获取私有属性
*/
public function __get($property_name){
if(isset($this->$property_name)){
return($this->$property_name);
}else{
return null;
}
}
/**
* __set()方法用来设置私有属性
*/
public function __set($property_name,$value){
$this->$property_name = $value;
}
/**
* 析构函数
*/
public function __destruct(){
$this->destroy();
$this->mysqli->close();
}
/**
* 函数执行错误处理
* @param string $functionName 函数名称
* @param array $args 包含信息的数组
* @return 返回执行状态
*/
public function __call($functionName,$args){
if(strstr($functionName,'getFieldBy')){
$search = str_replace('getFieldBy','',$functionName);
return $this->getFieldBy($args,$search);
}else if(!function_exists($functionName)){
$msg = "你所调用的函数: ".$functionName."不存在";
echo $msg;exit;
}
}
/**
* 销毁变量
* @return 无
*/
protected function destroy(){
$this->db_log($this->_sql());
if($type == 0){
$array = array('field'=>'','where'=>'','order'=>'','limit'=>'','table'=>'','pri'=>'');
}
foreach($array as $key=>$value){
unset($this->$key);
}
}
/**
* 关闭自动提交事务处理
* @param string $auto_commit 是否自动提交
* @return 返回当前对象
*/
public function auto_commit($auto_commit = ''){
$this->auto_commit = $auto_commit?$auto_commit:$this->auto_commit;
$this->mysqli->autocommit(FALSE);
return $this;
}
/**
* 事务提交
* @return boolean 布尔型结果
*/
public function commit(){
if(!$this->mysqli->error){
$result = $this->mysqli->commit();
if($result){
return true;
}else{
return null;
}
}else{
//事务回滚
$this->mysqli->rollback();
return null;
}
}
/**
* 给表加上前缀
* @param string $table 表名
* @return string 返回完整等我表名
*/
protected function db_press($table){
return "`".$this->db_press.$table."`";
}
/**
* 获取毫秒技术
* @param string $format [description]
* @param [type] $utimestamp [description]
* @return [type] [description]
*/
private function udate($format = 'u', $utimestamp = null) {
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
$milliseconds = round(($utimestamp - $timestamp) * 1000000);
return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
}
/**
* 写入日志
* @param string $sql 执行的sql语句
* @return 无
*/
private function db_log($sql){
if($this->db_log){
$file = 'cache/log.txt';
if(!file_exists($file)){
$myfile = fopen($file, "w");
}
$filesize = filesize($file)/1024/1024;//1M
if($filesize > 1){
file_put_contents($file,"");//防止日志太大
}
if(stripos("insert",'@'.$sql) || stripos("update",'@'.$sql)){
file_put_contents($file,$this->udate('Y-m-d H:i:s.u')." ".$sql."\r\n\r\n",FILE_APPEND | LOCK_EX);
}
}
}
/**
* 过滤字符
* @param string $item 需要过滤的字符
* @return string 返回过滤后的字符
*/
private function saddslashes($item){
$item=trim($item);