From 0890ad909b8330b02a00f73a054febe72da4d5be Mon Sep 17 00:00:00 2001 From: Nagy Imre Date: Thu, 28 Jan 2016 14:48:51 +0100 Subject: [PATCH 1/4] adding support for error codes --- MysqliDb.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/MysqliDb.php b/MysqliDb.php index 5c6adc2a..ad612cbc 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -106,6 +106,13 @@ class MysqliDb */ protected $_stmtError; + /** + * Variable which holds last statement error number + * + * @var int + */ + protected $_stmtErrno; + /** * Database credentials * @var string @@ -393,6 +400,7 @@ public function rawQuery($query, $bindParams = null) $stmt->execute(); $this->count = $stmt->affected_rows; $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $this->_lastQuery = $this->replacePlaceHolders($this->_query, $params); $res = $this->_dynamicBindResults($stmt); $this->reset(); @@ -464,6 +472,7 @@ public function query($query, $numRows = null) $stmt = $this->_buildQuery($numRows); $stmt->execute(); $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $res = $this->_dynamicBindResults($stmt); $this->reset(); @@ -555,6 +564,7 @@ public function get($tableName, $numRows = null, $columns = '*') $stmt->execute(); $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $res = $this->_dynamicBindResults($stmt); $this->reset(); @@ -676,6 +686,7 @@ public function update($tableName, $tableData, $numRows = null) $status = $stmt->execute(); $this->reset(); $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $this->count = $stmt->affected_rows; return $status; @@ -707,6 +718,7 @@ public function delete($tableName, $numRows = null) $stmt = $this->_buildQuery($numRows); $stmt->execute(); $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $this->reset(); return ($stmt->affected_rows > 0); @@ -1037,6 +1049,7 @@ private function _buildInsert($tableName, $insertData, $operation) $stmt = $this->_buildQuery(null, $insertData); $status = $stmt->execute(); $this->_stmtError = $stmt->error; + $this->_stmtErrno = $stmt->errno; $haveOnDuplicate = !empty ($this->_updateColumns); $this->reset(); $this->count = $stmt->affected_rows; @@ -1583,6 +1596,15 @@ public function getLastError() return trim($this->_stmtError . " " . $this->mysqli()->error); } + /** + * Method returns mysql error number + * + * @return int + */ + public function getLastErrno () { + return $this->_stmtErrno; + } + /** * Mostly internal method to get query and its params out of subquery object * after get() and getAll() From de1cc3290a8bd27313e4e76f29de2bee6573190d Mon Sep 17 00:00:00 2001 From: Ettemlevest Date: Fri, 22 Apr 2016 12:27:26 +0200 Subject: [PATCH 2/4] Typos --- MysqliDb.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index ad612cbc..939560a0 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -107,7 +107,7 @@ class MysqliDb protected $_stmtError; /** - * Variable which holds last statement error number + * Variable which holds last statement error code * * @var int */ @@ -1597,7 +1597,7 @@ public function getLastError() } /** - * Method returns mysql error number + * Method returns mysql error code * * @return int */ From 631decde44df60d97ae55933347da557b9256e49 Mon Sep 17 00:00:00 2001 From: Ettemlevest Date: Fri, 22 Apr 2016 12:29:11 +0200 Subject: [PATCH 3/4] Remove unwanted comment lines --- MysqliDb.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index 939560a0..55415e8c 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -108,7 +108,6 @@ class MysqliDb /** * Variable which holds last statement error code - * * @var int */ protected $_stmtErrno; @@ -1598,7 +1597,6 @@ public function getLastError() /** * Method returns mysql error code - * * @return int */ public function getLastErrno () { From a4ebb98b6d595ccf6828d5bbfa2a8789bf3c203b Mon Sep 17 00:00:00 2001 From: Ettemlevest Date: Fri, 22 Apr 2016 13:33:00 +0200 Subject: [PATCH 4/4] Adding error helper functions --- readme.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/readme.md b/readme.md index a313f9cd..f54ab988 100644 --- a/readme.md +++ b/readme.md @@ -19,6 +19,7 @@ MysqliDb -- Simple MySQLi wrapper and object mapper with prepared statements **[Has method](#has-method)** **[Helper Methods](#helper-methods)** **[Transaction Helpers](#transaction-helpers)** +**[Error Helpers](#error-helpers)** ### Installation To utilize this class, first import MysqliDb.php into your project, and require it. @@ -621,6 +622,17 @@ if (!$db->insert ('myTable', $insertData)) { } ``` +### Error helpers +After you executed a query you have options to check if there was an error. You can get the MySQL error string or the error code for the last executed query. +```php +$db->where('login', 'admin')->update('users', ['firstName' => 'Jack']); + +if ($db->getLastErrno() === 0) + echo 'Update succesfull'; +else + echo 'Update failed. Error: '. $db->getLastError(); +``` + ### Query exectution time benchmarking To track query execution time setTrace() function should be called. ```php