Commit | Line | Data |
---|---|---|
fb6aedc2 JB |
1 | <?php |
2 | ||
3 | /** | |
4 | * [CustomDB description] | |
5 | */ | |
6 | class CustomDB | |
7 | { | |
8 | private $connection; | |
9 | public $connected; | |
10 | private $current_pquery; | |
11 | private $current_stmt; | |
12 | ||
13 | /** | |
14 | * [__construct description] | |
15 | * @param [type] $host [description] | |
16 | * @param [type] $username [description] | |
17 | * @param [type] $password [description] | |
18 | * @param [type] $dbname [description] | |
19 | */ | |
20 | public function __construct($host, $username, $password, $dbname) | |
21 | { | |
22 | $connection = new mysqli($host, $username, $password, $dbname); | |
23 | $this->connection = $connection; | |
24 | ||
25 | if (!$this->connection->connect_errno) { | |
26 | $this->connected = true; | |
27 | } else { | |
28 | die('Fail to connect to the RDBMS'); | |
29 | } | |
30 | ||
31 | return $this->connection; | |
32 | } | |
33 | ||
34 | /** | |
35 | * [close description] | |
36 | * @return [type] [description] | |
37 | */ | |
38 | public function close() | |
39 | { | |
40 | if ($this->connected && $this->connection->close()) { | |
41 | $this->connected = false; | |
42 | } else { | |
43 | die('Fail to close the connection to the RDBMS'); | |
44 | } | |
45 | } | |
46 | ||
47 | /** | |
48 | * [query description] | |
49 | * @param [type] $sql_query [description] | |
50 | * @return [type] [description] | |
51 | */ | |
52 | public function query($sql_query) | |
53 | { | |
54 | if ($this->connected && !($query_result = $this->connection->query($sql_query))) { | |
b5f60f89 | 55 | echo "Fail to execute the SQL query : " . $sql_query . "<br>"; |
fb6aedc2 JB |
56 | } |
57 | return $query_result; | |
58 | } | |
59 | ||
60 | /** | |
61 | * [prepare_query description] | |
62 | * @param [type] $prepared_query [description] | |
63 | * @return [type] [description] | |
64 | */ | |
65 | public function prepare_query($prepared_query) | |
66 | { | |
67 | $this->current_pquery = $prepared_query; | |
68 | if ($this->connected && !($this->current_stmt = $this->connection->prepare($this->current_pquery))) { | |
69 | // Empty the currently stored prepared query in the failure case | |
70 | $this->current_pquery = ""; | |
b5f60f89 | 71 | echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
72 | } |
73 | return $this->current_stmt; | |
74 | } | |
75 | ||
76 | /** | |
77 | * [prepared_query_bind_param description] | |
78 | * @param [type] $params [description] | |
79 | * @return [type] [description] | |
80 | */ | |
81 | public function prepared_query_bind_param(...$params) | |
82 | { | |
b5f60f89 | 83 | var_dump($params); |
fb6aedc2 JB |
84 | $rt_val = $this->current_stmt->bind_param($params); |
85 | if (!$rt_val) { | |
b5f60f89 | 86 | echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
87 | } |
88 | return $rt_val; | |
89 | } | |
90 | ||
91 | /** | |
92 | * [run_prepared_query description] | |
93 | * @return [type] [description] | |
94 | */ | |
95 | public function run_prepared_query() | |
96 | { | |
97 | $rt_val = $this->current_stmt->execute(); | |
98 | if (!$rt_val) { | |
b5f60f89 | 99 | echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
100 | } |
101 | return $rt_val; | |
102 | } | |
103 | ||
104 | /** | |
105 | * [close_prepared_query description] | |
106 | * @return [type] [description] | |
107 | */ | |
108 | public function close_prepared_query() | |
109 | { | |
110 | $rt_val = $this->current_stmt->close(); | |
111 | if (!$rt_val) { | |
b5f60f89 | 112 | echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>"; |
fb6aedc2 JB |
113 | } |
114 | return $rt_val; | |
115 | } | |
116 | } | |
117 | ?> |