-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdo-right-way.php
More file actions
45 lines (38 loc) · 1.4 KB
/
pdo-right-way.php
File metadata and controls
45 lines (38 loc) · 1.4 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
<?php
try
{
$database = new class (
'mysql:host=localhost;dbname=mycms;charset=utf8', 'root', '',
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
])
extends PDO {
public function query($query, $arguments = false) {
if ($arguments === false) return parent::query($query);
$statement = parent::prepare($query);
$statement->execute($arguments);
return $statement;
}
public function create($table, $data = []) {
return $this->query('INSERT INTO ' . $table . ' (' . implode(', ', array_keys($data)) . ') VALUES (:' . implode(', :', array_keys($data)) .')', $data);
}
public function update($table, $data = [], $update_key, $update_value) {
$array = []; foreach($data as $key => $value) $array[] = $key .' = :' .$key; $data['update_value'] = $update_value;
return $this->query('UPDATE ' . $table . ' SET ' . implode(', ', $array) . ' WHERE '. $update_key .' = :update_value', $data);
}
public function delete($table, $delete_key, $delete_value) {
return $this->query('DELETE FROM ' . $table . ' WHERE ' . $delete_key . ' = ?', [$delete_value]);
}
};
$database->beginTransaction();
/* Twoje zapytania */
$database->commit();
}
catch(PDOException $e)
{
$database->rollBack();
/* Error handler */
}