diff --git a/MysqliDb.php b/MysqliDb.php index 872d327e..7ece626a 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -147,6 +147,13 @@ class MysqliDb protected $traceStripPrefix; public $trace = array(); + /** + * Variable for error tracking + */ + protected $errTrack = false; + protected $routeTrack; + protected $errMsg = ''; + /** * @param string $host * @param string $username @@ -227,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 * @@ -237,6 +275,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(); $this->_orderBy = array(); @@ -1192,6 +1242,7 @@ protected function replacePlaceHolders ($str, $vals) { $str = substr ($str, $pos + 1); } $newStr .= $str; + return $newStr; }