-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.class.php
More file actions
116 lines (93 loc) · 3.39 KB
/
database.class.php
File metadata and controls
116 lines (93 loc) · 3.39 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
110
111
112
113
114
115
116
<?php
namespace Library\Base;
use PDO;
class Database
{
private $connection;
function __construct($host, $port = 3306, $user = "root", $password = "", $database = "")
{
$options = array(
PDO::ATTR_TIMEOUT => 10,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
$this->connection = new PDO("mysql:host=$host;port=$port;dbname=$database", $user, $password, $options);
$this->tableMatrix();
}
private function tableMatrix()
{
$this->invokeCommand("CREATE TABLE IF NOT EXISTS test(firstName VARCHAR(256), lastName VARCHAR(256), email VARCHAR(256));");
}
public function writeNow($table, $columns = array(), $values = array())
{
$parameters = "";
for ($i = 0; $i < count($values); $i++) {
$parameters .= "?, ";
}
if ($this->endsWith($parameters, ", ")) {
$parameters = substr_replace($parameters, "", -2);
}
$columnResult = "";
foreach ($columns as $column) {
$columnResult .= $column . ', ';
}
if ($this->endsWith($columnResult, ", ")) {
$columnResult = substr_replace($columnResult, "", -2);
}
$this->connection->prepare("INSERT INTO $table($columnResult) VALUES($parameters);")->execute($values);
}
public function delete($table, $key, $value)
{
$statement = $this->connection->prepare("DELETE FROM $table WHERE $key=?");
$statement->execute(array($value));
}
public function queryAll($table)
{
$statement = $this->connection->prepare("SELECT * FROM $table");
$statement->execute();
return $statement->fetchAll();
}
public function query($table, $key, $value)
{
$statement = $this->connection->prepare("SELECT * FROM $table WHERE $key=?");
$statement->execute(array($value));
return $statement->fetch();
}
public function queryConditional($table, $key, $value, $key2, $value2)
{
$statement = $this->connection->prepare("SELECT * FROM $table WHERE $key=? AND $key2=?");
$statement->execute(array($value, $value2));
return $statement->fetchAll();
}
public function querySpecial($qry)
{
$statement = $this->connection->prepare($qry);
$statement->execute();
return $statement->fetchAll();
}
public function queryLength($table)
{
$statement = $this->connection->prepare("SELECT COUNT(*) FROM $table");
$statement->execute();
return $statement->fetchColumn();
}
public function queryLengthWhere($table, $key, $value)
{
$statement = $this->connection->prepare("SELECT COUNT(*) FROM $table WHERE $key=?");
$statement->execute(array($value));
return $statement->fetchColumn();
}
public function queryLike($table, $key, $pattern)
{
$statement = $this->connection->prepare("SELECT * FROM $table WHERE $key LIKE ?");
$statement->execute(array($pattern));
return $statement->fetchAll();
}
private function invokeCommand($command)
{
$this->connection->exec($command);
}
private function endsWith($string, $search)
{
return strrpos($string, $search) === strlen($string) - strlen($search);
}
}