Skip to content

Commit cc6b579

Browse files
committed
Workaround bug fix, which created new bug when using query_prepared method directly
In reference to #153
1 parent 142e421 commit cc6b579

File tree

2 files changed

+97
-28
lines changed

2 files changed

+97
-28
lines changed

lib/Database/ez_mysqli.php

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class ez_mysqli extends ezsqlModel implements DatabaseInterface
1212
{
13+
private $shortcutUsed = false;
1314
/**
1415
* Database connection handle
1516
* @var resource
@@ -213,7 +214,67 @@ private function fetch_prepared_result(&$stmt, $query)
213214

214215
// Take note of column info
215216
while($field = $meta->fetch_field())
216-
$variables[] = &$this->col_info[$field->name]; // pass by reference
217+
$variables[] = $this->col_info[$field->name];
218+
219+
// Binds variables to a prepared statement for result storage
220+
\call_user_func_array([$stmt, 'bind_result'], $variables);
221+
222+
$i = 0;
223+
// Store Query Results
224+
while($stmt->fetch()) {
225+
// Store results as an objects within main array
226+
foreach($this->col_info as $key => $value)
227+
$this->last_result[$i] = (object) array( $key => $value );
228+
$i++;
229+
}
230+
}
231+
232+
// If there is an error then take note of it..
233+
if ( $str = $stmt->error ) {
234+
$is_insert = true;
235+
$this->register_error($str);
236+
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
237+
238+
// If debug ALL queries
239+
$this->trace || $this->debug_all ? $this->debug() : null ;
240+
return false;
241+
}
242+
243+
// Return number of rows affected
244+
$return_val = $this->_affectedRows;
245+
246+
// disk caching of queries
247+
$this->store_cache($query, $is_insert);
248+
249+
// If debug ALL queries
250+
$this->trace || $this->debug_all ? $this->debug() : null ;
251+
252+
return $return_val;
253+
}
254+
255+
return false;
256+
}
257+
258+
private function prepared_result(&$stmt, $query)
259+
{
260+
if ($stmt instanceof \mysqli_stmt) {
261+
$stmt->store_result();
262+
$variables = array();
263+
$is_insert = false;
264+
if ( \preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
265+
$this->_affectedRows = \mysqli_stmt_affected_rows($stmt);
266+
267+
// Take note of the insert_id
268+
if ( \preg_match("/^(insert|replace)\s+/i", $query) ){
269+
$this->insert_id = $stmt->insert_id;
270+
}
271+
} else {
272+
$this->_affectedRows = $stmt->num_rows;
273+
$meta = $stmt->result_metadata();
274+
275+
// Take note of column info
276+
while($field = $meta->fetch_field())
277+
$variables[] = &$this->col_info[$field->name];
217278

218279
// Binds variables to a prepared statement for result storage
219280
\call_user_func_array([$stmt, 'bind_result'], $variables);
@@ -263,9 +324,9 @@ private function fetch_prepared_result(&$stmt, $query)
263324
*/
264325
public function query_prepared(string $query, array $param = null)
265326
{
266-
$stmt = $this->dbh->prepare($query);
327+
$stmt = $this->dbh->prepare($query);
267328
$params = [];
268-
$types = \array_reduce($param,
329+
$types = \array_reduce($param,
269330
function ($string, &$arg) use (&$params) {
270331
$params[] = &$arg;
271332
if (\is_float($arg))
@@ -276,20 +337,26 @@ function ($string, &$arg) use (&$params) {
276337
$string .= 's';
277338
else
278339
$string .= 'b';
279-
return $string;
340+
341+
return $string;
280342
}, ''
281343
);
282344

283345
\array_unshift($params, $types);
284346

285347
\call_user_func_array([$stmt, 'bind_param'], $params);
286-
287-
$result = ($stmt->execute()) ? $this->fetch_prepared_result($stmt, $query) : false;
348+
349+
if ($this->shortcutUsed === true)
350+
$result = ($stmt->execute()) ? $this->fetch_prepared_result($stmt, $query) : false;
351+
else
352+
$result = $stmt->execute() ? $this->prepared_result($stmt, $query) : false;
353+
//$result = $stmt->execute() ? $stmt->get_result() : false;
288354

289355
// free and closes a prepared statement
290356
$stmt->free_result();
291357
$stmt->close();
292358

359+
$this->shortcutUsed = false;
293360
return $result;
294361
}
295362

@@ -339,10 +406,12 @@ public function query(string $query, bool $use_prepare = false)
339406
}
340407

341408
// Perform the query via std mysql_query function..
342-
if (!empty($param) && \is_array($param) && ($this->isPrepareOn()))
343-
return $this->query_prepared($query, $param);
344-
else
345-
$this->result = \mysqli_query($this->dbh, $query);
409+
if (!empty($param) && \is_array($param) && ($this->isPrepareOn())) {
410+
$this->shortcutUsed = true;
411+
return $this->query_prepared($query, $param);
412+
}
413+
414+
$this->result = \mysqli_query($this->dbh, $query);
346415

347416
// If there is an error then take note of it..
348417
if ( $str = \mysqli_error($this->dbh) ) {

tests/mysqli/mysqliTest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -419,43 +419,40 @@ public function testSelecting()
419419
* @covers ezsql\ezQuery::drop
420420
*/
421421
public function testSelectingAndCreateTable()
422-
{
423-
$this->object->prepareOff();
422+
{
424423
$this->object->create('users',
425424
column('id', INTR, 11, PRIMARY),
426425
column('tel_num', INTR, 32, notNULL),
427426
column('user_name ', VARCHAR, 128),
428427
column('email', CHAR, 50)
429428
);
430429

431-
$this->object->insert('users', ['id'=> 1,
430+
$this->assertEquals(0, $this->object->insert('users', ['id'=> 1,
432431
'tel_num' => 123456,
433432
'email' => '[email protected]',
434-
'user_name ' => 'walker']
433+
'user_name ' => 'walker'])
435434
);
436435

437-
$this->object->insert('users', ['id'=> 2,
436+
$this->assertEquals(0, $this->object->insert('users', ['id'=> 2,
438437
'tel_num' => 654321,
439438
'email' => '[email protected]',
440-
'user_name ' => 'email']
439+
'user_name ' => 'email'])
441440
);
442441

443-
$this->object->insert('users', ['id'=> 3,
442+
$this->assertEquals(0, $this->object->insert('users', ['id'=> 3,
444443
'tel_num' => 456123,
445444
'email' => '[email protected]',
446-
'user_name ' => 'host']
445+
'user_name ' => 'host'])
447446
);
448447

449448
$result = $this->object->selecting('users', 'id, tel_num, email', eq('user_name ', 'walker'));
450-
451449
foreach ($result as $row) {
452450
$this->assertEquals(1, $row->id);
453451
$this->assertEquals(123456, $row->tel_num);
454452
$this->assertEquals('[email protected]', $row->email);
455453
}
456454

457-
$this->object->drop('users');
458-
$this->object->prepareOn();
455+
$this->object->drop('users');
459456
}
460457

461458
/**
@@ -547,30 +544,33 @@ public function testWhere()
547544
}
548545

549546
/**
547+
* @covers ezsql\ezQuery::drop
550548
* @covers ezsql\Database\ez_mysqli::query_prepared
551-
* @covers ezsql\Database\ez_mysqli::fetch_prepared_result
549+
* @covers ezsql\Database\ez_mysqli::prepared_result
552550
* @covers ezsql\Database\ez_mysqli::prepareValues
553551
*/
554552
public function testQuery_prepared() {
555-
$this->object->prepareOff();
553+
$this->object->prepareOff();
554+
$this->object->drop('prepare_test');
556555
$this->assertEquals(0,
557-
$this->object->create('unit_test',
558-
column('id', INTR, 11, notNULL, AUTO, PRIMARY),
556+
$this->object->create('prepare_test',
557+
column('id', INTR, 11, notNULL, PRIMARY),
559558
column('prepare_key', VARCHAR, 50))
560559
);
561560

562-
$result = $this->object->query_prepared('INSERT INTO unit_test( id, prepare_key ) VALUES( ?, ? )', [ 9, 'test 1']);
561+
$result = $this->object->query_prepared('INSERT INTO prepare_test( id, prepare_key ) VALUES( ?, ? )', [ 9, 'test 1']);
563562
$this->assertEquals(1, $result);
564563

565-
$this->object->query_prepared('SELECT prepare_key FROM unit_test WHERE id = ?', [9]);
564+
$this->object->query_prepared('SELECT prepare_key FROM prepare_test WHERE id = ?', [9]);
566565
$query = $this->object->queryResult();
567-
568566
foreach($query as $row) {
569567
$result = $row->prepare_key;
570568
}
571569

572570
$this->assertEquals('test 1', $this->object->queryResult()[0]->prepare_key);
573571
$this->assertEquals('test 1', $result);
572+
573+
$this->object->drop('prepare_test');
574574
} // testQuery_prepared
575575

576576
/**

0 commit comments

Comments
 (0)