forked from wdaweb/113php-2-11304-PHP_FILE_AND_GRAPHIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.php
149 lines (121 loc) · 3.04 KB
/
function.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
define("DBNAME","file");
/**
* 建立資料庫的連線變數
* @param string $db 資料庫名稱
* @return object
*/
function pdo($db){
$dsn="mysql:host=localhost;charset=utf8;dbname=$db";
$pdo=new PDO($dsn,'root','');
return $pdo;
}
/***
* 回傳指定資料表的所有資料
* @param string $table 資料表名稱
* @return array
*/
function all($table){
$pdo=pdo(DBNAME);
$sql="select * from $table";
$rows=$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
/**
* 回傳指定資料表的特定ID的單筆資料
* @param string $table 資料表名稱
* @param integer $id || array $id 資料表ID
* @return array
*/
function find($table,$id){
$sql="select * from $table where ";
$pdo=$pdo=pdo(DBNAME);
if(is_array($id)){
$tmp=[];
foreach($id as $key => $value){
//sprintf("`%s`='%s'",$key,$value);
$tmp[]="`$key`='$value'";
}
$sql=$sql.join(" && ",$tmp);
}else{
$sql=$sql . " `id`='$id'";
}
$row=$pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
return $row;
}
/**
* 刪除指定條件的資料
* @param string $table 資料表名稱
* @param array $id 條件(數字或陣列)
* @return boolean
*/
function del($table ,$id){
$sql="delete from $table where ";
$pdo=$pdo=pdo(DBNAME);
if(is_array($id)){
$tmp=[];
foreach($id as $key => $value){
//sprintf("`%s`='%s'",$key,$value);
$tmp[]="`$key`='$value'";
}
$sql=$sql.join(" && ",$tmp);
}else{
$sql=$sql . " id='$id'";
}
return $pdo->exec($sql);
}
/**
* 更新指定條件的資料
* @param string $table 資料表名稱
* @param array $array 更新的欄位及內容
* @param array || number $id 條件(數字或陣列)
* @return boolean
*/
function update($table,$array,$id){
$sql="update $table set ";
$pdo=$pdo=pdo(DBNAME);
$tmp=[];
foreach($array as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql=$sql . join(",",$tmp);
if(is_array($id)){
$tmp=[];
foreach($id as $key => $value){
$tmp[]="`$key`='$value'";
}
$sql=$sql . " where ".join(" && ",$tmp);
}else{
$sql=$sql . " where `id`='$id'";
}
return $pdo->exec($sql);
}
/**
* 新增資料
* @param string $table 資料表名稱
* @param string $cols 新增的欄位字串
* @param string $values 新增的值字串
* @return boolean
*/
function insert($table,$array){
$pdo=pdo(DBNAME);
$sql="insert into $table ";
$keys=array_keys($array);
$sql=$sql . "(`".join("`,`",$keys)."`) values ('".join("','",$array)."')";
return $pdo->exec($sql);
}
/**
* 列出陣列內容
*/
function dd($array){
echo "<pre>";
print_r($array);
echo "</pre>";
}
//insert("member",["acc"=>21,
// "pw"=>21,
// "email"=>"[email protected]",
// "tel"=>"0933254879"]);
//
//update('member',['email'=>'[email protected]'],['acc'=>'19','pw'=>'19']);
?>