先宣告全部函式都會用到的資料庫連線設定及建立PDO資料庫物件
1 2
| $dsn="mysql:host=localhost;charset=utf8;dbname=dbname"; $pdo=new PDO($dsn,'root','password');
|
all()-給定資料表名和條件後,會回傳符合條件的所有資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
function all($table,...$arg){ global $pdo;
$sql="SELECT * FROM $table ";
switch(count($arg)){ case 1:
if(is_array($arg[0])){
foreach($arg[0] as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql.=" WHERE ". implode(" AND " ,$tmp); }else{ $sql.=$arg[0]; } break; case 2:
foreach($arg[0] as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql.=" WHERE ". implode(" AND " ,$tmp) . $arg[1]; break;
}
return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
|
find()-會回傳資料表指定條件的單筆資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
function find($table,$arg){ global $pdo;
$sql="SELECT * FROM $table WHERE "; if(is_array($arg)){
foreach($arg as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql.=implode(" AND " ,$tmp);
}else{
$sql.=" `id`='$arg'";
}
return $pdo->query($sql)->fetch(PDO::FETCH_ASSOC); }
|
update()-給定資料表的條件後,會去更新相應的資料。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
function update($table,$cols){ global $pdo;
foreach($cols as $key => $value){
if($key!='id'){
$tmp[]="`$key`='$value'"; }
}
$sql.="UPDATE $table SET ".implode(" AND " ,$tmp)." WHERE `id`='{$cols['id']}'";
return $pdo->exec($sql); }
function update($table,$cols,$where){ global $pdo;
foreach($cols as $key => $value){
$tmp_cols[]="`$key`='$value'"; }
foreach($where as $key => $value){
$tmp_where[]="`$key`='$value'"; }
$sql.="UPDATE $table SET ".implode("," ,$tmp_cols)." WHERE `".implode(" AND " ,$tmp_where).;
return $pdo->exec($sql); }
|
insert()-給定資料內容後,會去新增資料到資料表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
function insert($table,$arg){ global $pdo;
$cols=implode("`,`",$array_keys($arg)); $values=implode("','",$arg);
$sql="INSERT INTO $table (`$cols`) VALUES('$values')";
return $pdo->exec($sql); }
|
del()-給定條件後,會去刪除指定的資料
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
function del($table,$arg){ global $pdo;
$sql="DELETE FROM $table WHERE "; if(is_array($arg)){
foreach($arg as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql.=implode(" AND " ,$tmp);
}else{
$sql.=" `id`='$arg'";
}
return $pdo->exec($sql); }
|
math()-使用指定的聚合函數進行資料表的計算或取值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
function math($table,$math,$col,...$arg){ global $pdo;
$sql="SELECT $math(`$col`) FROM $table ";
if(!empty($arg[0])){
foreach($arg[0] as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql.=" WHERE " . implode(" AND " ,$tmp);
}
return $pdo->query($sql)->fetchColumn(); }
|
to()-頁面導向,取代header(‘location:url’)
1 2 3 4 5 6 7 8 9
|
function to($url){
header("location:".$url);
}
|
q()-可以用來撰寫較為複雜的SQL語句,並回傳查詢結果的全部資料
1 2 3 4 5 6 7 8 9 10
|
function q($sql){ global $pdo; return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
|
save()-整合insert()和update()的功能為一支函式
如果以一次一筆資料來進行更新或新增時,我們可以發現兩個函式傳入的參數是類似的:
新增 - insert($table,$arg);
更新 - update($table,$arg);
其中兩個$arg的主要差異在於是否有 id 這個欄位,新增的資料不會 id 這個欄位,而更新單筆資料時通常會需要指定 id 因此利用這個差異,我們設計讓兩個功能整合在一支函式中,可以擴展自訂函式的功用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
function save($table,$arg){ global $pdo; if(isset($arg['id'])){
foreach($arg as $key => $value){
if($key!='id'){
$tmp[]="`$key`='$value'"; }
} $sql.="UPDATE $table SET ".implode(" , " ,$tmp)." WHERE `id`='{$arg['id']}'";
}else{ $cols=implode("`,`",$array_keys($arg)); $values=implode("','",$arg);
$sql="INSERT INTO $table (`$cols`) VALUES('$values')";
} return $pdo->exec($sql);
}
|