Skip to content

Commit a62e491

Browse files
committed
Merge pull request #466 from Ettemlevest/master
Adding support for mysql error codes
2 parents 95b27c5 + a4ebb98 commit a62e491

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

MysqliDb.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ class MysqliDb
106106
*/
107107
protected $_stmtError;
108108

109+
/**
110+
* Variable which holds last statement error code
111+
* @var int
112+
*/
113+
protected $_stmtErrno;
114+
109115
/**
110116
* Database credentials
111117
* @var string
@@ -393,6 +399,7 @@ public function rawQuery($query, $bindParams = null)
393399
$stmt->execute();
394400
$this->count = $stmt->affected_rows;
395401
$this->_stmtError = $stmt->error;
402+
$this->_stmtErrno = $stmt->errno;
396403
$this->_lastQuery = $this->replacePlaceHolders($this->_query, $params);
397404
$res = $this->_dynamicBindResults($stmt);
398405
$this->reset();
@@ -464,6 +471,7 @@ public function query($query, $numRows = null)
464471
$stmt = $this->_buildQuery($numRows);
465472
$stmt->execute();
466473
$this->_stmtError = $stmt->error;
474+
$this->_stmtErrno = $stmt->errno;
467475
$res = $this->_dynamicBindResults($stmt);
468476
$this->reset();
469477

@@ -555,6 +563,7 @@ public function get($tableName, $numRows = null, $columns = '*')
555563

556564
$stmt->execute();
557565
$this->_stmtError = $stmt->error;
566+
$this->_stmtErrno = $stmt->errno;
558567
$res = $this->_dynamicBindResults($stmt);
559568
$this->reset();
560569

@@ -676,6 +685,7 @@ public function update($tableName, $tableData, $numRows = null)
676685
$status = $stmt->execute();
677686
$this->reset();
678687
$this->_stmtError = $stmt->error;
688+
$this->_stmtErrno = $stmt->errno;
679689
$this->count = $stmt->affected_rows;
680690

681691
return $status;
@@ -707,6 +717,7 @@ public function delete($tableName, $numRows = null)
707717
$stmt = $this->_buildQuery($numRows);
708718
$stmt->execute();
709719
$this->_stmtError = $stmt->error;
720+
$this->_stmtErrno = $stmt->errno;
710721
$this->reset();
711722

712723
return ($stmt->affected_rows > 0);
@@ -1037,6 +1048,7 @@ private function _buildInsert($tableName, $insertData, $operation)
10371048
$stmt = $this->_buildQuery(null, $insertData);
10381049
$status = $stmt->execute();
10391050
$this->_stmtError = $stmt->error;
1051+
$this->_stmtErrno = $stmt->errno;
10401052
$haveOnDuplicate = !empty ($this->_updateColumns);
10411053
$this->reset();
10421054
$this->count = $stmt->affected_rows;
@@ -1583,6 +1595,14 @@ public function getLastError()
15831595
return trim($this->_stmtError . " " . $this->mysqli()->error);
15841596
}
15851597

1598+
/**
1599+
* Method returns mysql error code
1600+
* @return int
1601+
*/
1602+
public function getLastErrno () {
1603+
return $this->_stmtErrno;
1604+
}
1605+
15861606
/**
15871607
* Mostly internal method to get query and its params out of subquery object
15881608
* after get() and getAll()

readme.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ MysqliDb -- Simple MySQLi wrapper and object mapper with prepared statements
1919
**[Has method](#has-method)**
2020
**[Helper Methods](#helper-methods)**
2121
**[Transaction Helpers](#transaction-helpers)**
22+
**[Error Helpers](#error-helpers)**
2223

2324
### Installation
2425
To utilize this class, first import MysqliDb.php into your project, and require it.
@@ -621,6 +622,17 @@ if (!$db->insert ('myTable', $insertData)) {
621622
}
622623
```
623624

625+
### Error helpers
626+
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.
627+
```php
628+
$db->where('login', 'admin')->update('users', ['firstName' => 'Jack']);
629+
630+
if ($db->getLastErrno() === 0)
631+
echo 'Update succesfull';
632+
else
633+
echo 'Update failed. Error: '. $db->getLastError();
634+
```
635+
624636
### Query exectution time benchmarking
625637
To track query execution time setTrace() function should be called.
626638
```php

0 commit comments

Comments
 (0)