11<?php
22/**
3- * The database object which helps abstract database functions
3+ * The database object which helps to abstract database functions
44 *
55 * Uses and requires PDO, generally available after PHP 5.1
66 *
1111 * @link https://github.com/likel/php-simple-sessions
1212 * @version 1.0.0
1313 */
14- namespace Likel \ Session ;
14+ namespace Likel ;
1515
1616class DB
1717{
18- private $ dbh ;
19- private $ stmt ;
20- private $ table_prefix ;
18+ private $ database_handler ; // Stores the database connection
19+ private $ statement ; // The MySQL query with prepared values
20+ private $ table_prefix ; // The table prefix from the credentials.ini file
2121
2222 /**
2323 * Construct the database object
2424 *
2525 * @param string $credentials_location The location of the credential file
2626 * @return void
2727 */
28- public function __construct ($ credentials_location = __DIR__ . ' /../ini/credentials.ini ' )
28+ public function __construct ($ credentials_location )
2929 {
3030 try {
3131 $ db_credentials = parse_ini_file ($ credentials_location , true );
32- $ this ->dbh = $ this ->loadDatabase ($ db_credentials ["likel_db " ]);
32+ $ this ->database_handler = $ this ->loadDatabase ($ db_credentials ["likel_db " ]);
3333 $ this ->table_prefix = $ db_credentials ["likel_db " ]["table_prefix " ];
3434 } catch (\Exception $ ex ) {
3535 throw $ ex ;
3636 }
3737 }
3838
39- private function loadDatabase ($ credentials ) {
39+ /**
40+ * Attempt to retrieve the likel_db ini array and connect to the database
41+ *
42+ * @param array $credentials likel_db from the credentials.ini file
43+ * @return mixed
44+ * @throws \Exception If credentials empty or not found
45+ * @throws \PDOException If PDO connection is unsuccessful
46+ */
47+ private function loadDatabase ($ credentials )
48+ {
4049 if (!empty ($ credentials )){
4150 try {
4251 $ dsn = 'mysql:host= ' . $ credentials ['host ' ] . ';dbname= ' . $ credentials ['db_name ' ];
@@ -51,7 +60,7 @@ private function loadDatabase($credentials) {
5160 return $ pdo_object ;
5261
5362 } catch (\PDOException $ e ) {
54- throw new \Exception ($ e );
63+ throw new \Exception ($ e-> getMessage () );
5564 }
5665
5766 } else {
@@ -61,15 +70,23 @@ private function loadDatabase($credentials) {
6170
6271 /**
6372 * Prepare the query from a supplied query string
73+ *
74+ * @param string $query The prepared query
75+ * @return void
6476 */
6577 public function query ($ query )
6678 {
67- $ this ->stmt = $ this ->dbh ->prepare ($ query );
79+ $ this ->statement = $ this ->database_handler ->prepare ($ query );
6880 }
6981
7082 /**
7183 * Bind properties to the statement
7284 * E.G. $DB->bind(':fname', 'Liam');
85+ *
86+ * @param string $param The parameter to replace
87+ * @param mixed $value The value replacement
88+ * @param mixed $type Force the PDO::PARAM type
89+ * @return void
7390 */
7491 public function bind ($ param , $ value , $ type = null )
7592 {
@@ -89,46 +106,56 @@ public function bind($param, $value, $type = null)
89106 }
90107 }
91108
92- $ this ->stmt ->bindValue ($ param , $ value , $ type );
109+ $ this ->statement ->bindValue ($ param , $ value , $ type );
93110 }
94111
95112 /**
96113 * Execute the statement
97114 * Use result()/results() for insert queries
115+ *
116+ * @return bool
98117 */
99118 public function execute ()
100119 {
101- return $ this ->stmt ->execute ();
120+ return $ this ->statement ->execute ();
102121 }
103122
104123 /**
105124 * Return multiple rows
125+ *
126+ * @return array
106127 */
107128 public function results ()
108129 {
109130 $ this ->execute ();
110- return $ this ->stmt ->fetchAll (\PDO ::FETCH_ASSOC );
131+ return $ this ->statement ->fetchAll (\PDO ::FETCH_ASSOC );
111132 }
112133
113134 /**
114135 * Return a single row
136+ *
137+ * @return array
115138 */
116139 public function result ()
117140 {
118141 $ this ->execute ();
119- return $ this ->stmt ->fetch (\PDO ::FETCH_ASSOC );
142+ return $ this ->statement ->fetch (\PDO ::FETCH_ASSOC );
120143 }
121144
122145 /**
123146 * Return the row count
147+ *
148+ * @return int
124149 */
125150 public function rowCount ()
126151 {
127- return $ this ->stmt ->rowCount ();
152+ return $ this ->statement ->rowCount ();
128153 }
129154
130155 /**
131156 * Return if rows exists
157+ *
158+ * @return bool
132159 */
133160 public function rowsExist ()
134161 {
@@ -137,38 +164,49 @@ public function rowsExist()
137164
138165 /**
139166 * Return the id of the last inserted row
167+ *
168+ * @return mixed
140169 */
141170 public function lastInsertId ()
142171 {
143- return $ this ->dbh ->lastInsertId ();
172+ return $ this ->database_handler ->lastInsertId ();
144173 }
145174
146175 /**
147176 * Begin a transaction for multiple statements
177+ *
178+ * @return bool
148179 */
149180 public function beginTransaction ()
150181 {
151- return $ this ->dbh ->beginTransaction ();
182+ return $ this ->database_handler ->beginTransaction ();
152183 }
153184
154185 /**
155186 * Commit the transaction for multiple statements
187+ *
188+ * @return bool
156189 */
157190 public function endTransaction ()
158191 {
159- return $ this ->dbh ->commit ();
192+ return $ this ->database_handler ->commit ();
160193 }
161194
162195 /**
163196 * Roll back the transaction
197+ *
198+ * @return bool
164199 */
165200 public function cancelTransaction ()
166201 {
167- return $ this ->dbh ->rollBack ();
202+ return $ this ->database_handler ->rollBack ();
168203 }
169204
170205 /**
171206 * Return the table name with prefix
207+ *
208+ * @param string $table_name The table name that's accessed
209+ * @return string
172210 */
173211 public function getTableName ($ table_name )
174212 {
@@ -177,9 +215,11 @@ public function getTableName($table_name)
177215
178216 /**
179217 * Dump the statement's current parameters
218+ *
219+ * @return void
180220 */
181221 public function dumpStatement ()
182222 {
183- return $ this ->stmt ->debugDumpParams ();
223+ $ this ->statement ->debugDumpParams ();
184224 }
185225}
0 commit comments