From dfc63b2633a1026caca4eb7d873ededb1e2d3a8b Mon Sep 17 00:00:00 2001 From: Moatilliatta Date: Wed, 5 Aug 2015 15:02:27 -0500 Subject: [PATCH 1/5] trace errors into a file I've been into a requirement from my job, and i think this works well and I suppose that are other colleagues that would use this functions so they save all the error data related to MySQLi so here is my contribution to this excellent library methods: Main function to generate the error from querys: errorTrack($insertData = null,$case_err="") Function that help us to populate our file with all the data recolected from errorTrack function private function createLog($data) Properties: //Boolean variable to set the errorTrack method protected $errTrack = false; //String variable to set the route where the file will be created protected $routeTrack; Example of use /** * Execute sql sentence with parameters * @param string $sql SQL sentence * @param array $params All parameters necessary for the query * * @return int Result * */ public static function execute($sql,$params){ //Set the errorTrack method self::getInstance()->setErrTrack(true); //Execute Query $result = self::getInstance()->rawQuery($sql, $params); //Return result return $result; } This methods and properties have been tested with INSERT,UPDATE,DELETE sentences working OK. --- MysqliDb.php | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 872d327e..231b9078 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -147,6 +147,12 @@ class MysqliDb protected $traceStripPrefix; public $trace = array(); + /** + * Variable for error tracking + */ + protected $errTrack = false; + protected $routeTrack; + /** * @param string $host * @param string $username @@ -509,6 +515,12 @@ public function update($tableName, $tableData) $stmt = $this->_buildQuery (null, $tableData); $status = $stmt->execute(); + + //Save possible errors into log + if($this->errTrack === true){ + $this->errorTrack($tableData,"UPDATE"); + } + $this->reset(); $this->_stmtError = $stmt->error; $this->count = $stmt->affected_rows; @@ -534,6 +546,12 @@ public function delete($tableName, $numRows = null) $stmt = $this->_buildQuery($numRows); $stmt->execute(); + + //Save possible errors into log + if($this->errTrack === true){ + $this->errorTrack(); + } + $this->_stmtError = $stmt->error; $this->reset(); @@ -776,6 +794,70 @@ protected function _buildPair ($operator, $value) { return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias']; } + /** + * Just to verify if any possible error during insert + * @param [type] $insertData Data containing information for inserting into the DB + * @param string $case_err Case of error where it comes from + * @return [type] [description] + */ + private function errorTrack($insertData = null,$case_err=""){ + + $msg = NULL; + $query = null; + + if($this->_mysqli->error != NULL){ + + //Determinate which case is going to be evaluated + switch ($case_err) { + case "INSERT": case "UPDATE": + $values = ""; + + foreach ($insertData as $key => $value): + $values .= '"'.$value.'",'; + endforeach; + + $values = substr($values, 0,-1); + + $sql = explode('VALUES', $this->_query); + + $query = ($sql != NULL)? $sql[0] . ' VALUES('.$values.')' : ''; + + break; + + default: + $query = "Problem preparing query (".$this->_query.") " . + $this->mysqli()->error; + break; + } + + //Put the MySQLi errno in order to complement the error info + $msg = date("Y-m-d h:i:s ").'MySQLi errno: '.$this->_mysqli->errno.' - '. + $this->_mysqli->error." \nQuery: ".$query."\n\n"; + + $this->createLog($msg); + + } + + } + + /** + * Internal function to create a .txt file to save all the log + * collected from an specific query + * @param [type] $data Data that is going to be written in the file + */ + private function createLog($data){ + + //Guardamos directorio actual + $actual = getcwd(); + + $file = $this->routeTrack."mysqli_errors.txt"; + + $fh = fopen($file, 'a') or die("Can't open/create file"); + fwrite($fh,$data); + fclose($fh); + + } + /** * Internal function to build and execute INSERT/REPLACE calls * @@ -792,6 +874,12 @@ private function _buildInsert ($tableName, $insertData, $operation) $this->_query = $operation . " " . implode (' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName; $stmt = $this->_buildQuery (null, $insertData); $stmt->execute(); + + //Save possible errors into log + if($this->errTrack === true){ + $this->errorTrack($insertData,"INSERT"); + } + $this->_stmtError = $stmt->error; $this->reset(); $this->count = $stmt->affected_rows; @@ -1131,11 +1219,17 @@ protected function _buildLimit ($numRows) { */ protected function _prepareQuery() { + if (!$stmt = $this->mysqli()->prepare($this->_query)) { + + if($this->errTrack){ + $this->errorTrack(); + } + trigger_error("Problem preparing query ($this->_query) " . $this->mysqli()->error, E_USER_ERROR); } if ($this->traceEnabled) - $this->traceStartQ = microtime (true); + $this->traceStartQ = microtime (true); return $stmt; } @@ -1395,6 +1489,19 @@ public function setTrace ($enabled, $stripPrefix = null) { $this->traceStripPrefix = $stripPrefix; return $this; } + + /** + * Save errors into a file + * @param bool $enabled Allow create file to save errors + * @param string $route Set a route to create this file + */ + public function setErrTrack($enabled,$route=""){ + $this->errTrack = $enabled; + + // "/var/www/html/inc/libs/"; + $this->routeTrack = $route; + } + /** * Get where and what function was called for query stored in MysqliDB->trace * From faa21f8aad351c4a3d60384ab29cb72a2f85ef9e Mon Sep 17 00:00:00 2001 From: Moatilliatta Date: Thu, 6 Aug 2015 09:46:35 -0500 Subject: [PATCH 2/5] Update on errorTrack method Now the query shows all the parameters in its place, insted of ? symbols in the query. --- MysqliDb.php | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 231b9078..4f7b3f0c 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -518,7 +518,7 @@ public function update($tableName, $tableData) //Save possible errors into log if($this->errTrack === true){ - $this->errorTrack($tableData,"UPDATE"); + $this->errorTrack($this->_query,$tableData,"UPDATE"); } $this->reset(); @@ -800,41 +800,22 @@ protected function _buildPair ($operator, $value) { * @param string $case_err Case of error where it comes from * @return [type] [description] */ - private function errorTrack($insertData = null,$case_err=""){ + private function errorTrack($query = "",$insertData = null,$case_err=""){ $msg = NULL; - $query = null; if($this->_mysqli->error != NULL){ + + $qry = $this->replacePlaceHolders($query, $insertData); - //Determinate which case is going to be evaluated - switch ($case_err) { - case "INSERT": case "UPDATE": - $values = ""; - - foreach ($insertData as $key => $value): - $values .= '"'.$value.'",'; - endforeach; - - $values = substr($values, 0,-1); - - $sql = explode('VALUES', $this->_query); - - $query = ($sql != NULL)? $sql[0] . ' VALUES('.$values.')' : ''; - - break; - - default: - $query = "Problem preparing query (".$this->_query.") " . - $this->mysqli()->error; - break; - } + $query = "Problem preparing query (".$qry.") " . $this->mysqli()->error; - //Put the MySQLi errno in order to complement the error info - $msg = date("Y-m-d h:i:s ").'MySQLi errno: '.$this->_mysqli->errno.' - '. - $this->_mysqli->error." \nQuery: ".$query."\n\n"; + //Put the MySQLi errno in order to complement the error info + $msg = date("Y-m-d h:i:s ").'MySQLi errno: '. + $this->_mysqli->errno.' - '. + $this->_mysqli->error." \nQuery: ".$query."\n\n"; - $this->createLog($msg); + $this->createLog($msg); } @@ -877,7 +858,7 @@ private function _buildInsert ($tableName, $insertData, $operation) //Save possible errors into log if($this->errTrack === true){ - $this->errorTrack($insertData,"INSERT"); + $this->errorTrack($this->_query,$insertData,"INSERT"); } $this->_stmtError = $stmt->error; From 86cc0f85187c22e2e6e48a5ba672d04161f3e7e0 Mon Sep 17 00:00:00 2001 From: Moatilliatta Date: Thu, 6 Aug 2015 10:07:18 -0500 Subject: [PATCH 3/5] Update errorTrack It was showing the mysqli->error twice! Now it is corrected --- MysqliDb.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 4f7b3f0c..96a6524f 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -807,13 +807,11 @@ private function errorTrack($query = "",$insertData = null,$case_err=""){ if($this->_mysqli->error != NULL){ $qry = $this->replacePlaceHolders($query, $insertData); - - $query = "Problem preparing query (".$qry.") " . $this->mysqli()->error; //Put the MySQLi errno in order to complement the error info $msg = date("Y-m-d h:i:s ").'MySQLi errno: '. $this->_mysqli->errno.' - '. - $this->_mysqli->error." \nQuery: ".$query."\n\n"; + $this->_mysqli->error." \nQuery: ".$qry."\n\n"; $this->createLog($msg); @@ -1306,7 +1304,7 @@ public function getSubQuery () { 'alias' => $this->host ); $this->reset(); - return $val; + return $val;e } /* Helper functions */ From 8d22a9bbd282977683b1a2b049dea71e744a4051 Mon Sep 17 00:00:00 2001 From: Moatilliatta Date: Mon, 10 Aug 2015 10:21:48 -0500 Subject: [PATCH 4/5] Using less function and adding one attribute --- MysqliDb.php | 56 ++++++++++++---------------------------------------- 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 96a6524f..ad3e7063 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -152,6 +152,7 @@ class MysqliDb */ protected $errTrack = false; protected $routeTrack; + protected $errMsg = ''; /** * @param string $host @@ -242,6 +243,18 @@ protected function reset() { if ($this->traceEnabled) $this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller()); + + if($this->errTrack && $this->_mysqli->error != NULL){ + + //Put the MySQLi errno in order to complement the error info + $this->errMsg = date("Y-m-d h:i:s ").'MySQLi errno: '. + $this->_mysqli->errno.' - '. + $this->_mysqli->error." \nQuery: ". + $this->_lastQuery."\n\n"; + + $this->createLog($this->errMsg); + + } $this->_where = array(); $this->_join = array(); @@ -515,12 +528,6 @@ public function update($tableName, $tableData) $stmt = $this->_buildQuery (null, $tableData); $status = $stmt->execute(); - - //Save possible errors into log - if($this->errTrack === true){ - $this->errorTrack($this->_query,$tableData,"UPDATE"); - } - $this->reset(); $this->_stmtError = $stmt->error; $this->count = $stmt->affected_rows; @@ -546,12 +553,6 @@ public function delete($tableName, $numRows = null) $stmt = $this->_buildQuery($numRows); $stmt->execute(); - - //Save possible errors into log - if($this->errTrack === true){ - $this->errorTrack(); - } - $this->_stmtError = $stmt->error; $this->reset(); @@ -793,31 +794,6 @@ protected function _buildPair ($operator, $value) { return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias']; } - - /** - * Just to verify if any possible error during insert - * @param [type] $insertData Data containing information for inserting into the DB - * @param string $case_err Case of error where it comes from - * @return [type] [description] - */ - private function errorTrack($query = "",$insertData = null,$case_err=""){ - - $msg = NULL; - - if($this->_mysqli->error != NULL){ - - $qry = $this->replacePlaceHolders($query, $insertData); - - //Put the MySQLi errno in order to complement the error info - $msg = date("Y-m-d h:i:s ").'MySQLi errno: '. - $this->_mysqli->errno.' - '. - $this->_mysqli->error." \nQuery: ".$qry."\n\n"; - - $this->createLog($msg); - - } - - } /** * Internal function to create a .txt file to save all the log @@ -853,12 +829,6 @@ private function _buildInsert ($tableName, $insertData, $operation) $this->_query = $operation . " " . implode (' ', $this->_queryOptions) ." INTO " .self::$prefix . $tableName; $stmt = $this->_buildQuery (null, $insertData); $stmt->execute(); - - //Save possible errors into log - if($this->errTrack === true){ - $this->errorTrack($this->_query,$insertData,"INSERT"); - } - $this->_stmtError = $stmt->error; $this->reset(); $this->count = $stmt->affected_rows; From 4fb8473e8085ca66e9144b47213dd36d200882c4 Mon Sep 17 00:00:00 2001 From: Moatilliatta Date: Mon, 10 Aug 2015 10:24:46 -0500 Subject: [PATCH 5/5] remove extra code --- MysqliDb.php | 75 ++++++++++++++++++++++++---------------------------- 1 file changed, 35 insertions(+), 40 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index ad3e7063..7ece626a 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -234,6 +234,37 @@ public static function getInstance() return self::$_instance; } + + /** + * Save errors into a file + * @param bool $enabled Allow create file to save errors + * @param string $route Set a route to create this file + */ + public function setErrTrack($enabled,$route=""){ + $this->errTrack = $enabled; + + // "/var/www/html/inc/libs/"; + $this->routeTrack = $route; + } + + /** + * Internal function to create a .txt file to save all the log + * collected from an specific query + * @param [type] $data Data that is going to be written in the file + */ + protected function createLog($data){ + + //Guardamos directorio actual + $actual = getcwd(); + + $file = $this->routeTrack."mysqli_errors.txt"; + + $fh = fopen($file, 'a') or die("Can't open/create file"); + fwrite($fh,$data); + fclose($fh); + + } + /** * Reset states after an execution * @@ -243,7 +274,7 @@ protected function reset() { if ($this->traceEnabled) $this->trace[] = array ($this->_lastQuery, (microtime(true) - $this->traceStartQ) , $this->_traceGetCaller()); - + if($this->errTrack && $this->_mysqli->error != NULL){ //Put the MySQLi errno in order to complement the error info @@ -793,24 +824,6 @@ protected function _buildPair ($operator, $value) { $this->_bindParams ($subQuery['params']); return " " . $operator . " (" . $subQuery['query'] . ") " . $subQuery['alias']; - } - - /** - * Internal function to create a .txt file to save all the log - * collected from an specific query - * @param [type] $data Data that is going to be written in the file - */ - private function createLog($data){ - - //Guardamos directorio actual - $actual = getcwd(); - - $file = $this->routeTrack."mysqli_errors.txt"; - - $fh = fopen($file, 'a') or die("Can't open/create file"); - fwrite($fh,$data); - fclose($fh); - } /** @@ -1168,17 +1181,11 @@ protected function _buildLimit ($numRows) { */ protected function _prepareQuery() { - if (!$stmt = $this->mysqli()->prepare($this->_query)) { - - if($this->errTrack){ - $this->errorTrack(); - } - trigger_error("Problem preparing query ($this->_query) " . $this->mysqli()->error, E_USER_ERROR); } if ($this->traceEnabled) - $this->traceStartQ = microtime (true); + $this->traceStartQ = microtime (true); return $stmt; } @@ -1235,6 +1242,7 @@ protected function replacePlaceHolders ($str, $vals) { $str = substr ($str, $pos + 1); } $newStr .= $str; + return $newStr; } @@ -1274,7 +1282,7 @@ public function getSubQuery () { 'alias' => $this->host ); $this->reset(); - return $val;e + return $val; } /* Helper functions */ @@ -1438,19 +1446,6 @@ public function setTrace ($enabled, $stripPrefix = null) { $this->traceStripPrefix = $stripPrefix; return $this; } - - /** - * Save errors into a file - * @param bool $enabled Allow create file to save errors - * @param string $route Set a route to create this file - */ - public function setErrTrack($enabled,$route=""){ - $this->errTrack = $enabled; - - // "/var/www/html/inc/libs/"; - $this->routeTrack = $route; - } - /** * Get where and what function was called for query stored in MysqliDB->trace *