Skip to content

Adding support for mysql error codes #466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ class MysqliDb
*/
protected $_stmtError;

/**
* Variable which holds last statement error code
* @var int
*/
protected $_stmtErrno;

/**
* Database credentials
* @var string
Expand Down Expand Up @@ -393,6 +399,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();
Expand Down Expand Up @@ -464,6 +471,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();

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

$stmt->execute();
$this->_stmtError = $stmt->error;
$this->_stmtErrno = $stmt->errno;
$res = $this->_dynamicBindResults($stmt);
$this->reset();

Expand Down Expand Up @@ -676,6 +685,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;
Expand Down Expand Up @@ -707,6 +717,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);
Expand Down Expand Up @@ -1037,6 +1048,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;
Expand Down Expand Up @@ -1583,6 +1595,14 @@ public function getLastError()
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()
Expand Down
12 changes: 12 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down