Small cleanups.
[Project_webapp.git] / lib / db.php
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 private $current_result;
13
14 /**
15 * [__construct description]
16 * @param [type] $host [description]
17 * @param [type] $username [description]
18 * @param [type] $password [description]
19 * @param [type] $dbname [description]
20 */
21 public function __construct($host, $username, $password, $dbname)
22 {
23 $this->connection = new mysqli($host, $username, $password, $dbname);
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 /* public function __destruct()
35 {
36 $this->close();
37 } */
38
39 /**
40 * [close description]
41 * @return [type] [description]
42 */
43 public function close()
44 {
45 if ($this->connected && $this->connection->close()) {
46 $this->connected = false;
47 } else {
48 die('Fail to close the connection to the RDBMS.');
49 }
50 }
51
52 /**
53 * [query description]
54 * @param [type] $sql_query [description]
55 * @return [type] [description]
56 */
57 public function query($sql_query)
58 {
59 if ($this->connected && !($this->current_result = $this->connection->query($sql_query))) {
60 echo "Fail to execute the SQL query : " . $sql_query . "<br>";
61 }
62 return $this->current_result;
63 }
64
65 /**
66 * [prepare_query description]
67 * @param [type] $prepared_query [description]
68 * @return [type] [description]
69 */
70 public function prepare_query($prepared_query)
71 {
72 $this->current_pquery = $prepared_query;
73 if ($this->connected && !($this->current_stmt = $this->connection->prepare($this->current_pquery))) {
74 // Empty the currently stored prepared query in the failure case
75 $this->current_pquery = "";
76 echo "Fail to prepare SQL query : (" . $this->connection->errno . ") " . $this->connection->error . " - " . $this->current_pquery . "<br>";
77 }
78 return $this->current_stmt;
79 }
80
81 /**
82 * [prepared_query_bind_param description]
83 * @param [type] $params [description]
84 * @return [type] [description]
85 */
86 public function prepared_query_bind_param($types, $params)
87 {
88 $rt_val = $this->current_stmt->bind_param($types, ...$params);
89 if (!$rt_val) {
90 echo "Fail to link parameters to SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
91 }
92 return $rt_val;
93 }
94
95 /**
96 * [run_prepared_query description]
97 * @return [type] [description]
98 */
99 public function run_prepared_query()
100 {
101 $rt_val = $this->current_stmt->execute();
102 if (!$rt_val) {
103 echo "Fail to execute SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
104 }
105 return $rt_val;
106 }
107
108 public function get_pquery_result()
109 {
110 $rt_val = $this->current_result = $this->current_stmt->get_result();
111 if (!$rt_val) {
112 echo "Fail to fill SQL query result : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
113 }
114 return $rt_val;
115 }
116
117 public function get_result_array()
118 {
119 $rt_val = $this->current_result->fetch_array();
120 if (!$rt_val) {
121 echo "Fail to build SQL query result array : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
122 }
123 return $rt_val;
124 }
125
126 /**
127 * [close_prepared_query description]
128 * @return [type] [description]
129 */
130 public function close_prepared_query()
131 {
132 $rt_val = $this->current_stmt->close();
133 if (!$rt_val) {
134 echo "Fail to close SQL query : (" . $this->current_stmt->errno . ") " . $this->current_stmt->error . " - " . $this->current_pquery . "<br>";
135 }
136 return $rt_val;
137 }
138 }
139 ?>