Commit a62e491a authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #466 from Ettemlevest/master

Adding support for mysql error codes
parents 95b27c5a a4ebb98b
...@@ -106,6 +106,12 @@ class MysqliDb ...@@ -106,6 +106,12 @@ class MysqliDb
*/ */
protected $_stmtError; protected $_stmtError;
/**
* Variable which holds last statement error code
* @var int
*/
protected $_stmtErrno;
/** /**
* Database credentials * Database credentials
* @var string * @var string
...@@ -393,6 +399,7 @@ class MysqliDb ...@@ -393,6 +399,7 @@ class MysqliDb
$stmt->execute(); $stmt->execute();
$this->count = $stmt->affected_rows; $this->count = $stmt->affected_rows;
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$this->_lastQuery = $this->replacePlaceHolders($this->_query, $params); $this->_lastQuery = $this->replacePlaceHolders($this->_query, $params);
$res = $this->_dynamicBindResults($stmt); $res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
...@@ -464,6 +471,7 @@ class MysqliDb ...@@ -464,6 +471,7 @@ class MysqliDb
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$res = $this->_dynamicBindResults($stmt); $res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
...@@ -555,6 +563,7 @@ class MysqliDb ...@@ -555,6 +563,7 @@ class MysqliDb
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$res = $this->_dynamicBindResults($stmt); $res = $this->_dynamicBindResults($stmt);
$this->reset(); $this->reset();
...@@ -676,6 +685,7 @@ class MysqliDb ...@@ -676,6 +685,7 @@ class MysqliDb
$status = $stmt->execute(); $status = $stmt->execute();
$this->reset(); $this->reset();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$this->count = $stmt->affected_rows; $this->count = $stmt->affected_rows;
return $status; return $status;
...@@ -707,6 +717,7 @@ class MysqliDb ...@@ -707,6 +717,7 @@ class MysqliDb
$stmt = $this->_buildQuery($numRows); $stmt = $this->_buildQuery($numRows);
$stmt->execute(); $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$this->reset(); $this->reset();
return ($stmt->affected_rows > 0); return ($stmt->affected_rows > 0);
...@@ -1037,6 +1048,7 @@ class MysqliDb ...@@ -1037,6 +1048,7 @@ class MysqliDb
$stmt = $this->_buildQuery(null, $insertData); $stmt = $this->_buildQuery(null, $insertData);
$status = $stmt->execute(); $status = $stmt->execute();
$this->_stmtError = $stmt->error; $this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$haveOnDuplicate = !empty ($this->_updateColumns); $haveOnDuplicate = !empty ($this->_updateColumns);
$this->reset(); $this->reset();
$this->count = $stmt->affected_rows; $this->count = $stmt->affected_rows;
...@@ -1583,6 +1595,14 @@ class MysqliDb ...@@ -1583,6 +1595,14 @@ class MysqliDb
return trim($this->_stmtError . " " . $this->mysqli()->error); return trim($this->_stmtError . " " . $this->mysqli()->error);
} }
/**
* Method returns mysql error code
* @return int
*/
public function getLastErrno () {
return $this->_stmtErrno;
}
/** /**
* Mostly internal method to get query and its params out of subquery object * Mostly internal method to get query and its params out of subquery object
* after get() and getAll() * after get() and getAll()
......
...@@ -19,6 +19,7 @@ MysqliDb -- Simple MySQLi wrapper and object mapper with prepared statements ...@@ -19,6 +19,7 @@ MysqliDb -- Simple MySQLi wrapper and object mapper with prepared statements
**[Has method](#has-method)** **[Has method](#has-method)**
**[Helper Methods](#helper-methods)** **[Helper Methods](#helper-methods)**
**[Transaction Helpers](#transaction-helpers)** **[Transaction Helpers](#transaction-helpers)**
**[Error Helpers](#error-helpers)**
### Installation ### Installation
To utilize this class, first import MysqliDb.php into your project, and require it. To utilize this class, first import MysqliDb.php into your project, and require it.
...@@ -621,6 +622,17 @@ if (!$db->insert ('myTable', $insertData)) { ...@@ -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 ### Query exectution time benchmarking
To track query execution time setTrace() function should be called. To track query execution time setTrace() function should be called.
```php ```php
......
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