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
*/
protected $_stmtError;
/**
* Variable which holds last statement error code
* @var int
*/
protected $_stmtErrno;
/**
* Database credentials
* @var string
......@@ -393,6 +399,7 @@ class MysqliDb
$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 +471,7 @@ class MysqliDb
$stmt = $this->_buildQuery($numRows);
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$res = $this->_dynamicBindResults($stmt);
$this->reset();
......@@ -555,6 +563,7 @@ class MysqliDb
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$res = $this->_dynamicBindResults($stmt);
$this->reset();
......@@ -676,6 +685,7 @@ class MysqliDb
$status = $stmt->execute();
$this->reset();
$this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$this->count = $stmt->affected_rows;
return $status;
......@@ -707,6 +717,7 @@ class MysqliDb
$stmt = $this->_buildQuery($numRows);
$stmt->execute();
$this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$this->reset();
return ($stmt->affected_rows > 0);
......@@ -1037,6 +1048,7 @@ class MysqliDb
$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 +1595,14 @@ class MysqliDb
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
* after get() and getAll()
......
......@@ -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
......
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