11<?php
22/**
3- * This is the DB object
3+ * The database object which helps abstract database functions
4+ *
5+ * Uses and requires PDO, generally available after PHP 5.1
46 *
57 * @package php-simple-sessions
68 * @author Liam Kelly <https://github.com/likel>
@@ -15,142 +17,150 @@ class DB
1517{
1618 private $ dbh ;
1719 private $ stmt ;
20+ private $ table_prefix ;
1821
22+ /**
23+ * Construct the database object
24+ *
25+ * @param string $credentials_location The location of the credential file
26+ * @return void
27+ */
1928 public function __construct ($ credentials_location = __DIR__ . '/../ini/credentials.ini ' )
2029 {
2130 try {
2231 $ db_credentials = parse_ini_file ($ credentials_location , true );
23- $ this ->dbh = loadDatabase ($ db_credentials ["likel_db " ]);
24- } catch (Exception $ ex ) {
32+ $ this ->dbh = $ this ->loadDatabase ($ db_credentials ["likel_db " ]);
33+ $ this ->table_prefix = $ db_credentials ["likel_db " ]["table_prefix " ];
34+ } catch (\Exception $ ex ) {
2535 throw $ ex ;
2636 }
2737 }
2838
2939 private function loadDatabase ($ credentials ) {
30- if (!empty ($ credentials[ " likel_db " ] )){
40+ if (!empty ($ credentials )){
3141 try {
32- $ dsn = 'mysql:host= ' . $ credentials [" likel_db " ][ 'host ' ] . ';dbname= ' . $ credentials[ " likel_db " ] ['db_name ' ];
42+ $ dsn = 'mysql:host= ' . $ credentials ['host ' ] . ';dbname= ' . $ credentials ['db_name ' ];
3343
3444 $ options = array (
35- PDO ::ATTR_PERSISTENT => true ,
36- PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION
45+ \ PDO ::ATTR_PERSISTENT => true ,
46+ \ PDO ::ATTR_ERRMODE => \ PDO ::ERRMODE_EXCEPTION
3747 );
3848
39- $ pdo_object = new PDO ($ dsn , $ credentials [" likel_db " ][ 'username ' ], $ credentials[ " likel_db " ] ['password ' ], $ options );
49+ $ pdo_object = new \ PDO ($ dsn , $ credentials ['username ' ], $ credentials ['password ' ], $ options );
4050
4151 return $ pdo_object ;
4252
43- } catch (PDOException $ e ) {
44- throw new Exception (' An error occured when attempting to connect to the database. ' );
53+ } catch (\ PDOException $ e ) {
54+ throw new \ Exception ($ e );
4555 }
4656
4757 } else {
48- throw new Exception ('The credential file could not be located or is empty. ' );
58+ throw new \ Exception ('The credential file could not be located or is empty. ' );
4959 }
5060 }
5161
5262 /**
53- * Prepare the query from a supplied query string.
63+ * Prepare the query from a supplied query string
5464 */
5565 public function query ($ query )
5666 {
5767 $ this ->stmt = $ this ->dbh ->prepare ($ query );
5868 }
5969
6070 /**
61- * Bind properties to the statement.
71+ * Bind properties to the statement
6272 * E.G. $DB->bind(':fname', 'Liam');
6373 */
6474 public function bind ($ param , $ value , $ type = null )
6575 {
6676 if (is_null ($ type )) {
6777 switch (true ) {
6878 case is_int ($ value ):
69- $ type = PDO ::PARAM_INT ;
79+ $ type = \ PDO ::PARAM_INT ;
7080 break ;
7181 case is_bool ($ value ):
72- $ type = PDO ::PARAM_BOOL ;
82+ $ type = \ PDO ::PARAM_BOOL ;
7383 break ;
7484 case is_null ($ value ):
75- $ type = PDO ::PARAM_NULL ;
85+ $ type = \ PDO ::PARAM_NULL ;
7686 break ;
7787 default :
78- $ type = PDO ::PARAM_STR ;
88+ $ type = \ PDO ::PARAM_STR ;
7989 }
8090 }
8191
8292 $ this ->stmt ->bindValue ($ param , $ value , $ type );
8393 }
8494
8595 /**
86- * Execute the statement.
87- * Use result()/results() for insert queries.
96+ * Execute the statement
97+ * Use result()/results() for insert queries
8898 */
8999 public function execute ()
90100 {
91101 return $ this ->stmt ->execute ();
92102 }
93103
94104 /**
95- * Return multiple rows.
105+ * Return multiple rows
96106 */
97107 public function results ()
98108 {
99109 $ this ->execute ();
100- return $ this ->stmt ->fetchAll (PDO ::FETCH_ASSOC );
110+ return $ this ->stmt ->fetchAll (\ PDO ::FETCH_ASSOC );
101111 }
102112
103113 /**
104- * Return a single row.
114+ * Return a single row
105115 */
106116 public function result ()
107117 {
108118 $ this ->execute ();
109- return $ this ->stmt ->fetch (PDO ::FETCH_ASSOC );
119+ return $ this ->stmt ->fetch (\ PDO ::FETCH_ASSOC );
110120 }
111121
112122 /**
113- * Return the row count.
123+ * Return the row count
114124 */
115125 public function rowCount ()
116126 {
117127 return $ this ->stmt ->rowCount ();
118128 }
119129
120130 /**
121- * Return if rows exists.
131+ * Return if rows exists
122132 */
123133 public function rowsExist ()
124134 {
125135 return $ this ->rowCount () != 0 ;
126136 }
127137
128138 /**
129- * Return the id of the last inserted row.
139+ * Return the id of the last inserted row
130140 */
131141 public function lastInsertId ()
132142 {
133143 return $ this ->dbh ->lastInsertId ();
134144 }
135145
136146 /**
137- * Begin a transaction for multiple statements.
147+ * Begin a transaction for multiple statements
138148 */
139149 public function beginTransaction ()
140150 {
141151 return $ this ->dbh ->beginTransaction ();
142152 }
143153
144154 /**
145- * Commit the transaction for multiple statements.
155+ * Commit the transaction for multiple statements
146156 */
147157 public function endTransaction ()
148158 {
149159 return $ this ->dbh ->commit ();
150160 }
151161
152162 /**
153- * Roll back the transaction.
163+ * Roll back the transaction
154164 */
155165 public function cancelTransaction ()
156166 {
@@ -160,13 +170,13 @@ public function cancelTransaction()
160170 /**
161171 * Return the table name with prefix
162172 */
163- public function tb ( $ tablename )
173+ public function getTableName ( $ table_name )
164174 {
165- return $ this ->tableprefix . $ tablename ;
175+ return $ this ->table_prefix . $ table_name ;
166176 }
167177
168178 /**
169- * Dump the statement's current parameters.
179+ * Dump the statement's current parameters
170180 */
171181 public function dumpStatement ()
172182 {
0 commit comments