-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
70 lines (70 loc) · 2.05 KB
/
Copy pathdb.php
File metadata and controls
70 lines (70 loc) · 2.05 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
<?php
class database{
public $db;
private $sql;
private $table;
private $query;
public function __construct($dbname, $user, $pass)
{
try{
$this->db = new PDO("mysql:host=localhost;dbname=$dbname",$user, $pass);
}catch(PDOException $e){
echo $e->getMessage();
die();
}
}
public function select($table, $WHERE=NULL) {
$this->table = $table;
$this->sql = "SELECT * FROM $table $WHERE";
if($WHERE != NULL){
$this->sql = "SELECT * FROM $table WHERE $WHERE";
}
return $this;
}
public function like($value, $like){
$this->sql = "SELECT * FROM $this->table WHERE $value LIKE '%$like%'";
return $this;
}
public function insert($table, $columns){
$val = array();
$col = array();
foreach ($columns as $column => $value){
$val[] = $value;
$col[] = $column;
}
$col = implode(',', $col);
for($i = 0; $i < count($val); $i++){
$val[$i] = "'".$val[$i]."'";
}
$val = implode(',', $val);
$this->sql = "INSERT INTO $table ($col) VALUES ($val)";
return $this;
}
public function update($table, $where, $array){
$val = array();
$col = array();
$all = array();
foreach ($array as $arra => $value){
$val[] = $value;
$col[] = $arra;
}
for($i = 0; $i < count($val); $i++){
$val[$i] = "'".$val[$i]."'";
}
for($i = 0; $i < count($val); $i++){
$all[] = "$col[$i] = $val[$i]";
}
$all = implode(',',$all);
$this->sql = "UPDATE $table SET $all WHERE $where";
return $this;
}
public function remove($table, $where){
$this->sql = "DELETE FROM $table WHERE $where";
return $this;
}
public function get(){
$this->query = $this->db->prepare($this->sql);
$this->query->execute();
return $this->query->fetchAll(PDO::FETCH_ASSOC);
}
}