Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了数据库操作函数 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions _lp/core/lib/db.function.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,98 @@ function close_db( $db = NULL )
mysql_close( $db );
}

// ==================================================================
//
// 插入数据
//
// ------------------------------------------------------------------


function db_insert($table,$data,$replace=false){
if(is_string($data)){
$data=db_create($data);
}
$keys=array_keys($data);
$values=array_values($data);
$values=array_map('s', $values);//安全过滤
$type=$replace?'REPLACE':'INSERT';
return run_sql("{$type} INTO `{$table}`(`".implode("`,`", $keys)."`) VALUES('".implode("','", $values)."')");
}

// ==================================================================
//
// 更新数据
//
// ------------------------------------------------------------------

function db_update($table,$data,$where){
if(is_string($data)){
$data=db_create($data);
}
$fields=array();
foreach($data as $key=>$value){
$fields[]="`{$key}`='".s($value)."'";//安全过滤
}
return run_sql("UPDATE `{$table}` SET ".implode(',', $fields)." WHERE {$where}");
}

// ==================================================================
//
// 创建数据
// $data的格式:
// field1,field2,field3
// or
// fiedl1,field2,field3:fun // 使用过滤函数过滤
// or
// field1,^field2,field3|fun,filed4:fun
// or
//!field1,field2,filed3:fun
//
// ------------------------------------------------------------------

function db_create($data){
//判断是否为非模式,字符串以!开头
if('!'==substr($data, 0,1)){
$not=true;
//如果为非模式,去掉开头的!
$data=substr($data,1);
}else{
$not=false;
}
$field_filter=explode(':', $data);
//提取字段
$fields=explode(',', $field_filter[0]);
//提取过滤函数
$filter=isset($field_filter[1])?$field_filter[1]:false;
$postData=$_POST;
if($not){//非模式的处理
foreach ($fields as $field ){
unset($postData[$field]);
}
if($filter) $postData=array_map($filter, $postData);
return $postData;
}
$result=array();
//循环字段
foreach ($fields as $field) {
$not_filter=false;
//字段如以^开头,不进行过滤函数过滤。
if('^'==substr($field, 0,1)){
$not_filter=true;
$field=substr($field, 1);
}
//字段如果有|隔开一个函数名还需要用单独的过滤函数过滤
$field_single=explode('|', $field);
$field=$field_single[0];
$single_filter=isset($field_single[1])?$field_single[1]:false;
//数据过滤
if($single_filter) $postData[$field]=$single_filter($postData[$field]);
if($filter) $postData[$field]=$filter($postData[$field]);
//加入result
$result[$field]=$postData[$field];
}
return $result;

}

?>
94 changes: 94 additions & 0 deletions _lp/core/lib/db.sae.function.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,98 @@ function close_db( $db = NULL )
mysql_close( $db );
}

// ==================================================================
//
// 插入数据
//
// ------------------------------------------------------------------


function db_insert($table,$data,$replace=false){
if(is_string($data)){
$data=db_create($data);
}
$keys=array_keys($data);
$values=array_values($data);
$values=array_map('s', $values);//安全过滤
$type=$replace?'REPLACE':'INSERT';
return run_sql("{$type} INTO `{$table}`(`".implode("`,`", $keys)."`) VALUES('".implode("','", $values)."')");
}

// ==================================================================
//
// 更新数据
//
// ------------------------------------------------------------------

function db_update($table,$data,$where){
if(is_string($data)){
$data=db_create($data);
}
$fields=array();
foreach($data as $key=>$value){
$fields[]="`{$key}`='".s($value)."'";//安全过滤
}
return run_sql("UPDATE `{$table}` SET ".implode(',', $fields)." WHERE {$where}");
}

// ==================================================================
//
// 创建数据
// $data的格式:
// field1,field2,field3
// or
// fiedl1,field2,field3:fun // 使用过滤函数过滤
// or
// field1,^field2,field3|fun,filed4:fun
// or
//!field1,field2,filed3:fun
//
// ------------------------------------------------------------------

function db_create($data){
//判断是否为非模式,字符串以!开头
if('!'==substr($data, 0,1)){
$not=true;
//如果为非模式,去掉开头的!
$data=substr($data,1);
}else{
$not=false;
}
$field_filter=explode(':', $data);
//提取字段
$fields=explode(',', $field_filter[0]);
//提取过滤函数
$filter=isset($field_filter[1])?$field_filter[1]:false;
$postData=$_POST;
if($not){//非模式的处理
foreach ($fields as $field ){
unset($postData[$field]);
}
if($filter) $postData=array_map($filter, $postData);
return $postData;
}
$result=array();
//循环字段
foreach ($fields as $field) {
$not_filter=false;
//字段如以^开头,不进行过滤函数过滤。
if('^'==substr($field, 0,1)){
$not_filter=true;
$field=substr($field, 1);
}
//字段如果有|隔开一个函数名还需要用单独的过滤函数过滤
$field_single=explode('|', $field);
$field=$field_single[0];
$single_filter=isset($field_single[1])?$field_single[1]:false;
//数据过滤
if($single_filter) $postData[$field]=$single_filter($postData[$field]);
if($filter) $postData[$field]=$filter($postData[$field]);
//加入result
$result[$field]=$postData[$field];
}
return $result;

}

?>