Commit 4f02f9df authored by Jeffrey Way's avatar Jeffrey Way

Added additional abstraction and error handling

parent 2191bf3b
...@@ -19,13 +19,12 @@ class MysqlDB { ...@@ -19,13 +19,12 @@ class MysqlDB {
* @param int $numRows The number of rows total to return. * @param int $numRows The number of rows total to return.
* @return array Contains the returned rows from the query. * @return array Contains the returned rows from the query.
*/ */
public function query($query, $numRows = NULL) public function query($query)
{ {
$this->_query = filter_var($query, FILTER_SANITIZE_STRING); $this->_query = filter_var($query, FILTER_SANITIZE_STRING);
$stmt = $this->_mysql->prepare($this->_query); $stmt = $this->_prepareQuery();
$stmt->execute(); $stmt->execute();
$results = $this->_dynamicBindResults($stmt); $results = $this->_dynamicBindResults($stmt);
return $results; return $results;
} }
...@@ -152,13 +151,13 @@ class MysqlDB { ...@@ -152,13 +151,13 @@ class MysqlDB {
* @return object Returns the $stmt object. * @return object Returns the $stmt object.
*/ */
protected function _buildQuery($numRows = NULL, $tableData = false) { protected function _buildQuery($numRows = NULL, $tableData = false) {
$hasTableData = null;
if ( gettype($tableData) === 'array') { if ( gettype($tableData) === 'array') {
$hasTableData = true; $hasTableData = true;
} }
// Did the user call the "where" method? // Did the user call the "where" method?
if (!empty($this->_where)) { if ( !empty($this->_where) ) {
$keys = array_keys($this->_where); $keys = array_keys($this->_where);
$where_prop = $keys[0]; $where_prop = $keys[0];
$where_value = $this->_where[$where_prop]; $where_value = $this->_where[$where_prop];
...@@ -220,7 +219,7 @@ class MysqlDB { ...@@ -220,7 +219,7 @@ class MysqlDB {
} }
// Prepare query // Prepare query
$stmt = $this->_mysql->prepare($this->_query) or die('Problem preparing query.'); $stmt = $this->_prepareQuery();
// Bind parameters // Bind parameters
if ( $hasTableData ) { if ( $hasTableData ) {
...@@ -233,7 +232,7 @@ class MysqlDB { ...@@ -233,7 +232,7 @@ class MysqlDB {
} }
else { else {
$stmt->bind_param($this->_paramTypeList, $where_value); if ( $this->_where ) $stmt->bind_param($this->_paramTypeList, $where_value);
} }
return $stmt; return $stmt;
...@@ -270,4 +269,13 @@ class MysqlDB { ...@@ -270,4 +269,13 @@ class MysqlDB {
return $results; return $results;
} }
protected function _prepareQuery()
{
if ( !$stmt = $this->_mysql->prepare($this->_query) ) {
trigger_error("Connection issue", E_USER_ERROR);
}
return $stmt;
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment