Skip to content

Commit 216ea29

Browse files
committed
:octocat: catch mysqli stmt error
1 parent 5259476 commit 216ea29

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Drivers/MySQLiDrv.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ protected function raw_query(string $sql, string $index = null, bool $assoc = nu
103103

104104
if(is_bool($result)){
105105

106-
if(!$result){
107-
throw new DriverException($this->db->error);
106+
if($this->db->errno !== 0 || !$result){
107+
throw new DriverException($this->db->error, $this->db->errno);
108108
}
109109

110110
return $result; // @codeCoverageIgnore
@@ -122,6 +122,7 @@ protected function prepared_query(string $sql, array $values = null, string $ind
122122
$assoc = $assoc ?? true;
123123
$stmt = $this->db->stmt_init();
124124
$stmt->prepare($sql);
125+
$this->stmtError($this->db->errno, $this->db->error);
125126

126127
if(count($values) > 0){
127128
call_user_func_array([$stmt, 'bind_param'], $this->getReferences($values));
@@ -177,6 +178,7 @@ protected function prepared_query(string $sql, array $values = null, string $ind
177178
protected function multi_query(string $sql, array $values){
178179
$stmt = $this->db->stmt_init();
179180
$stmt->prepare($sql);
181+
$this->stmtError($this->db->errno, $this->db->error);
180182

181183
foreach($values as $row){
182184
call_user_func_array([$stmt, 'bind_param'], $this->getReferences($row));
@@ -193,6 +195,7 @@ protected function multi_query(string $sql, array $values){
193195
protected function multi_callback_query(string $sql, iterable $data, $callback){
194196
$stmt = $this->db->stmt_init();
195197
$stmt->prepare($sql);
198+
$this->stmtError($this->db->errno, $this->db->error);
196199

197200
foreach($data as $k => $row){
198201
$row = call_user_func_array($callback, [$row, $k]);
@@ -209,6 +212,20 @@ protected function multi_callback_query(string $sql, iterable $data, $callback){
209212
return true;
210213
}
211214

215+
/**
216+
* @param int $errno
217+
* @param string $errstr
218+
*
219+
* @throws \chillerlan\Database\Drivers\DriverException
220+
*/
221+
protected function stmtError(int $errno, string $errstr):void{
222+
223+
if($errno !== 0){
224+
throw new DriverException($errstr, $errno);
225+
}
226+
227+
}
228+
212229
/**
213230
* Copies an array to an array of referenced values
214231
*

0 commit comments

Comments
 (0)