php根据数据库生成接口 用php创建数据库( 二 )


}elseif(!empty($this-data['condition'])){
//'预留WHERE', 'order', 'group', 'limit' …………等条件关键词处理接口
$where = $where ? "WHERE {$where}" : "WHERE 1";
isset($this-data['condition']['where'])$where .=' AND '.$this-data['condition']['where'];
isset($this-data['condition']['group'])$where .=' GROUP BY '.$this-data['condition']['group'];
isset($this-data['condition']['order'])$where .=' ORDER BY '.$this-data['condition']['order'];
isset($this-data['condition']['limit'])$where .=' LIMIT '.$this-data['condition']['limit'];
}else{
$where = "WHERE {$where}";
}
$this-condition = $where;
return $this;
}
//插入数据
public function insert($data = https://www.04ip.com/post/array(), $replace = false) {
$fields = $this-implodefields($data);
$insert = $replace ? 'REPLACE' : 'INSERT';
$sql = "{$insert} INTO `{$this-db}`.`{$this-table}` (".implode(', ',$fields[1]).") values (".implode(', ',$fields[2]).")";
$this-query($sql);
return $this-getInsertId();
}
//更新数据
public function update($data = https://www.04ip.com/post/array() ,$where ='') {
$numargs = func_num_args();
if ($numargs == 1) {
$where = $data;
$data = https://www.04ip.com/post/array();
}
$fields = $this-implodefields($data);
$this-condition($where);
$sql = "UPDATE `{$this-db}`.`{$this-table}` SET ".implode(', ',$fields[0])." {$this-condition}";
$this-query($sql);
return $this-getAffectedRows();
}
//删除数据
public function delete($where = NULL) {
if(!is_array($where)strtolower(substr(trim($where), 0, 6)) == 'delete'){
$sql = $where;
}else{
$this-condition($where);
$sql = "DELETEFROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
$this-query($sql);
return $this-getAffectedRows();
}
//查询数据
public function select($where = NULL, $fields = '*') {
if(!is_array($where)strtolower(substr(trim($where), 0, 6)) == 'select'){
$sql = $where;
}else{
$this-condition($where);
$sql = "SELECT {$fields} FROM `{$this-db}`.`{$this-table}` {$this-condition}";
}
return $this-fetch($this-query($sql));
}
//查询一条数据
public function getOne($where,$fields = '*') {
$data = https://www.04ip.com/post/$this-select($where,$fields ='*');
if($data) {
return$data[0];
}
return array();
}
//查询多条数据
public function getAll($where,$fields = '*') {
$data = https://www.04ip.com/post/$this-select($where,$fields ='*');
return$data;
}
//结果数量
public function getCount($where = '',$fields = '*') {
$this-condition($where);
$sql = "SELECT count({$fields}) as count FROM `{$this-db}`.`{$this-table}` {$this-condition}";
$data = https://www.04ip.com/post/$this-query($sql);
if($data){
return @mysql_result($data,0);
}
return 0;
}
//执行sql语句(flag为0返回mysql_query查询后的结果 , 为1返回lastid,其php根据数据库生成接口他返回影响行数,默认为2返回影响行数)
public function query($sql, $flag = '0', $type = '') {
if ($this-config['dbDebug']) {
$startime = $this-microtime_float();
}
//查询
if ($type == 'UNBUFFERED'function_exists('mysql_unbuffered_query')) {
$result = @mysql_unbuffered_query($sql, self::$link);
} else {
//exit($sql);
$result = @mysql_query($sql, self::$link);
}
//重试
if (in_array(mysql_errno(self::$link), array(2006,2013))empty($result)$this-config['dbPconnect']==0!defined('RETRY')) {
define('RETRY',true); @mysql_close(self::$link); sleep(2);
$this-connect();
$result = $this-query($sql);
}
if ($result === false) {
$this-errdie($sql);

推荐阅读