Skip to content

Commit 25001d9

Browse files
committed
updates and revert parts of last bug fix,
In reference to #153
1 parent 0658328 commit 25001d9

File tree

4 files changed

+56
-114
lines changed

4 files changed

+56
-114
lines changed

lib/Database/ez_mysqli.php

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

1111
class ez_mysqli extends ezsqlModel implements DatabaseInterface
1212
{
13-
private $shortcutUsed = false;
1413
/**
1514
* Database connection handle
1615
* @var resource
@@ -201,6 +200,7 @@ private function fetch_prepared_result(&$stmt, $query)
201200
$stmt->store_result();
202201
$variables = array();
203202
$is_insert = false;
203+
$col_info = array();
204204
if ( \preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
205205
$this->_affectedRows = \mysqli_stmt_affected_rows($stmt);
206206

@@ -213,81 +213,26 @@ private function fetch_prepared_result(&$stmt, $query)
213213
$meta = $stmt->result_metadata();
214214

215215
// Take note of column info
216-
while($field = $meta->fetch_field())
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++;
216+
while($field = $meta->fetch_field()) {
217+
$variables[] = &$col_info[$field->name];
229218
}
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];
278219

279220
// Binds variables to a prepared statement for result storage
280221
\call_user_func_array([$stmt, 'bind_result'], $variables);
281222

223+
$x = 0;
282224
$i = 0;
283225
// Store Query Results
284226
while($stmt->fetch()) {
285227
// Store results as an objects within main array
286-
foreach($this->col_info as $key => $value)
228+
foreach($col_info[$x] as $key => $value) {
287229
$this->last_result[$i] = (object) array( $key => $value );
288-
$i++;
230+
$i++;
231+
}
232+
$x++;
289233
}
290-
}
234+
$this->col_info = $col_info;
235+
}
291236

292237
// If there is an error then take note of it..
293238
if ( $str = $stmt->error ) {
@@ -313,7 +258,7 @@ private function prepared_result(&$stmt, $query)
313258
}
314259

315260
return false;
316-
}
261+
}
317262

318263
/**
319264
* Creates a prepared query, binds the given parameters and returns the result of the executed
@@ -346,17 +291,12 @@ function ($string, &$arg) use (&$params) {
346291

347292
\call_user_func_array([$stmt, 'bind_param'], $params);
348293

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;
294+
$result = ($stmt->execute()) ? $this->fetch_prepared_result($stmt, $query) : false;
354295

355296
// free and closes a prepared statement
356297
$stmt->free_result();
357298
$stmt->close();
358299

359-
$this->shortcutUsed = false;
360300
return $result;
361301
}
362302

@@ -407,7 +347,6 @@ public function query(string $query, bool $use_prepare = false)
407347

408348
// Perform the query via std mysql_query function..
409349
if (!empty($param) && \is_array($param) && ($this->isPrepareOn())) {
410-
$this->shortcutUsed = true;
411350
return $this->query_prepared($query, $param);
412351
}
413352

@@ -437,8 +376,8 @@ public function query(string $query, bool $use_prepare = false)
437376
// Return number of rows affected
438377
$return_val = $this->_affectedRows;
439378
} else {
379+
// Query was a select
440380
if ( !\is_numeric($this->result) && !\is_bool($this->result)) {
441-
// Query was a select
442381

443382
// Take note of column info
444383
$i = 0;

lib/ezsqlModel.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,41 @@ class ezsqlModel extends ezQuery implements ezsqlModelInterface
8787
* Log number of rows the query returned
8888
* @var int Default is null
8989
*/
90-
protected $num_rows = null;
90+
protected $num_rows = null;
9191

92-
protected $db_connect_time = 0;
93-
protected $sql_log_file = false;
94-
protected $profile_times = array();
92+
protected $db_connect_time = 0;
93+
protected $sql_log_file = false;
94+
protected $profile_times = array();
9595

9696
/**
9797
* ID generated from the AUTO_INCREMENT of the previous INSERT operation (if any)
9898
* @var int
9999
*/
100-
protected $insert_id = null;
100+
protected $insert_id = null;
101101

102102
/**
103103
* Use to keep track of the last query for debug..
104104
* @var string
105105
*/
106-
protected $last_query = null;
106+
protected $last_query = null;
107107

108108
/**
109109
* Use to keep track of last error
110110
* @var string
111111
*/
112-
protected $last_error = null;
112+
protected $last_error = null;
113113

114114
/**
115115
* Saved info on the table column
116116
* @var mixed
117117
*/
118-
protected $col_info = array();
118+
protected $col_info = array();
119119

120-
protected $timers = array();
120+
protected $timers = array();
121121
protected $total_query_time = 0;
122-
protected $trace_log = array();
123-
protected $use_trace_log = false;
124-
protected $do_profile = false;
122+
protected $trace_log = array();
123+
protected $use_trace_log = false;
124+
protected $do_profile = false;
125125

126126
/**
127127
* The last query result
@@ -246,7 +246,7 @@ public function flush()
246246
{
247247
// Get rid of these
248248
$this->last_result = null;
249-
$this->col_info = null;
249+
$this->col_info = array();
250250
$this->last_query = null;
251251
$this->from_disk_cache = false;
252252
$this->clearPrepare();
@@ -516,34 +516,34 @@ public function debug($print_to_screen = true)
516516
// Start output buffering
517517
\ob_start();
518518

519-
echo "<blockquote>";
519+
echo "\n\n<blockquote>";
520520

521521
// Only show ezSQL credits once..
522522
if ( ! $this->debug_called ) {
523-
echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
523+
echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.")\n <b>Debug.. \n</b></font><p>";
524524
}
525525

526526
if ( $this->last_error ) {
527-
echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error</b></font>]<p>";
527+
echo "<font face=arial size=2 color=000099><b>Last Error --</b> [<font color=000000><b>$this->last_error \n</b></font>]<p>";
528528
}
529529

530530
if ( $this->from_disk_cache ) {
531-
echo "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>";
531+
echo "<font face=arial size=2 color=000099><b>Results retrieved from disk cache</b></font><p>\n";
532532
}
533533

534-
echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] <b>--</b> ";
535-
echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
534+
echo "<font face=arial size=2 color=000099><b>Query</b> [$this->num_queries] \n<b>--</b>";
535+
echo "[<font color=000000><b>$this->last_query \n</b></font>]</font><p>";
536536

537-
echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
538-
echo "<blockquote>";
537+
echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>\n";
538+
echo "<blockquote>\n";
539539

540540
if ( $this->col_info ) {
541541
// Results top rows
542-
echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
543-
echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
542+
echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>\n";
543+
echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>\n";
544544

545545
for ( $i=0, $j=count($this->col_info); $i < $j; $i++ ) {
546-
echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>";
546+
echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>\n";
547547
/* when selecting count(*) the maxlengh is not set, size is set instead. */
548548
if (isset($this->col_info[$i]->type))
549549
echo "{$this->col_info[$i]->type}";
@@ -554,36 +554,36 @@ public function debug($print_to_screen = true)
554554
if (isset($this->col_info[$i]->max_length))
555555
echo "{$this->col_info[$i]->max_length}";
556556

557-
echo "</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>";
557+
echo "\n</font><br><span style='font-family: arial; font-size: 10pt; font-weight: bold;'>";
558558

559559
if (isset($this->col_info[$i]->name))
560560
echo "{$this->col_info[$i]->name}";
561561

562-
echo "</span></td>";
562+
echo "\n</span></td>";
563563
}
564-
echo "</tr>";
564+
echo "</tr>\n";
565565

566566
// print main results
567567
if ( $this->last_result ) {
568568
$i = 0;
569569
foreach ( $this->get_results(null, ARRAY_N) as $one_row ) {
570570
$i++;
571-
echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
571+
echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i \n</font></td>";
572572

573573
foreach ( $one_row as $item ) {
574-
echo "<td nowrap><font face=arial size=2>$item</font></td>";
574+
echo "<td nowrap><font face=arial size=2>$item \n</font></td>";
575575
}
576-
echo "</tr>";
576+
echo "</tr>\n";
577577
}
578578
} else {
579579
// if last result
580-
echo "<tr bgcolor=ffffff><td colspan=".(\count($this->col_info) + 1)."><font face=arial size=2>No Results</font></td></tr>";
580+
echo "<tr bgcolor=ffffff><td colspan=".(\count($this->col_info) + 1)."><font face=arial size=2>No Results</font></td></tr>\n";
581581
}
582582

583-
echo "</table>";
583+
echo "</table>\n";
584584
} else {
585585
// if col_info
586-
echo "<font face=arial size=2>No Results</font>";
586+
echo "<font face=arial size=2>No Results \n</font>";
587587
}
588588

589589
//echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";

tests/ezsqlModelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function testFlush()
105105

106106
$this->assertNull($this->object->getLast_Result());
107107
$this->assertNull($this->object->getLast_Query());
108-
$this->assertNull($this->object->getCol_Info());
108+
$this->assertIsArray($this->object->getCol_Info());
109109
$this->assertFalse($this->object->getFrom_Disk_Cache());
110110
} // testFlush
111111

tests/mysqli/mysqliTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ public function testSelecting()
424424
*/
425425
public function testSelectingAndCreateTable()
426426
{
427+
$this->object->drop('users');
427428
$this->object->create('users',
428429
column('id', INTR, 11, PRIMARY),
429430
column('tel_num', INTR, 32, notNULL),
@@ -450,6 +451,7 @@ public function testSelectingAndCreateTable()
450451
);
451452

452453
$result = $this->object->selecting('users', 'id, tel_num, email', eq('user_name ', 'walker'));
454+
//$this->object->debug(true);
453455
foreach ($result as $row) {
454456
$this->assertEquals(1, $row->id);
455457
$this->assertEquals(123456, $row->tel_num);
@@ -551,11 +553,12 @@ public function testWhere()
551553
/**
552554
* @covers ezsql\ezQuery::drop
553555
* @covers ezsql\Database\ez_mysqli::query_prepared
554-
* @covers ezsql\Database\ez_mysqli::prepared_result
556+
* @covers ezsql\Database\ez_mysqli::fetch_prepared_result
555557
* @covers ezsql\Database\ez_mysqli::prepareValues
556558
*/
557559
public function testQuery_prepared() {
558560
$this->object->prepareOff();
561+
$this->object->select(self::TEST_DB_NAME);
559562
$this->object->drop('prepare_test');
560563
$this->assertEquals(0,
561564
$this->object->create('prepare_test',
@@ -566,15 +569,15 @@ public function testQuery_prepared() {
566569
$result = $this->object->query_prepared('INSERT INTO prepare_test( id, prepare_key ) VALUES( ?, ? )', [ 9, 'test 1']);
567570
$this->assertEquals(1, $result);
568571

569-
$this->object->query_prepared('SELECT prepare_key FROM prepare_test WHERE id = ?', [9]);
572+
$this->object->query_prepared('SELECT id, prepare_key FROM prepare_test WHERE id = ?', [9]);
573+
570574
$query = $this->object->queryResult();
575+
//$this->object->debug(true);
571576
foreach($query as $row) {
572-
$result = $row->prepare_key;
577+
$this->assertEquals(9, $row->id);
578+
$this->assertEquals('test 1', $row->prepare_key);
573579
}
574580

575-
$this->assertEquals('test 1', $this->object->queryResult()[0]->prepare_key);
576-
$this->assertEquals('test 1', $result);
577-
578581
$this->object->drop('prepare_test');
579582
} // testQuery_prepared
580583

0 commit comments

Comments
 (0)