-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdo.php
More file actions
110 lines (103 loc) · 3.13 KB
/
Copy pathpdo.php
File metadata and controls
110 lines (103 loc) · 3.13 KB
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
<?php
class DB
{
/*
*@var host: host name
*@var user:database user name
*@var pass:password
*@var db:database name
*/
function __construct($host, $user, $pass, $dbn)
{
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbn = $dbn;
$this->dsn = "mysql:dbname=$dbn;host=$host";
}
//pdo section
function pdo_connect()
{
try {
$pdo = new PDO($this->dsn, $this->user, $this->pass);
}catch(PDOException $e) {
var_dump($e->getMessage());
die();
}
return $pdo;
}
//select(one) section
function pdo_select_one($sql)
{
if(empty($sql) || !$sql) {
$err["pdo_err_select"] = "SQL文が入力されませんでした。";
}else {
$result_select = array();
$td_select = $this->pdo_connect();
$stmt = $td_select->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result_select = $row;
}
return $result_select;
}
}
//select(all) section
function pdo_select_all($sql)
{
if(empty($sql) || !$sql) {
$err["pdo_err_select"] = "SQL文が入力されませんでした。";
}else {
$result_select = array();
$i = 0;
$td_select = $this->pdo_connect();
$stmt = $td_select->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result_select[] = $row;
$i++;
}
return $result_select;
}
}
//insert section
function pdo_insert($sql,$bindArray)
{
if(empty($sql) || !$sql) {
$err["pdo_err_insert"] = "SQL文が入力されませんでした。";
return $err;
}else {
$td_insert = $this->pdo_connect();
$stmt = $td_insert->prepare($sql);
if(!empty($bindArray)){
foreach($bindArray as $bindParam){
$stmt->bindValue($bindParam['param'], $bindParam['val'], $bindParam['type']);
}
$stmt->execute();
}else{
$err["pdo_insert_bindArray"] = "配列データが取得できませんでした";
return $err;
}
}
}
//update section
function pdo_update($sql,$bindArray)
{
if(empty($sql) || !$sql) {
$err["pdo_err_update"] = "SQL文が入力されませんでした。";
return $err;
}else {
$td_update = $this->pdo_connect();
$stmt = $td_update->prepare($sql);
if(!empty($bindArray)){
foreach($bindArray as $bindParam){
$stmt->bindValue($bindParam['param'], $bindParam['val'], $bindParam['type']);
}
$stmt->execute();
}else{
$err["pdo_insert_bindArray"] = "配列データが取得できませんでした";
return $err;
}
}
}
}