Skip to content

Commit f4ef632

Browse files
author
uzi88
committed
Improved functionalities
- fetchQueryToArray (added fetch first row) - arrayToInsert (added multirow insert) - added current_timestamp to reserved words
1 parent 43a674b commit f4ef632

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

MySQL_wrapper.class.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class MySQL_wrapper {
114114
/** Reserved words for array to ( insert / update )
115115
* @var array
116116
*/
117-
var $reserved = array('null', 'now()', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()');
117+
var $reserved = array('null', 'now()', 'current_timestamp', 'curtime()', 'localtime()', 'localtime', 'utc_date()', 'utc_time()', 'utc_timestamp()');
118118

119119
/** Constructor
120120
* @param string $server - MySQL Host name
@@ -267,14 +267,26 @@ function fetchArray($query = 0) {
267267

268268
/** Returns array with fetched associative rows.
269269
* @param string $sql - MySQL Query
270+
* @param string $fetchFirst - Fetch only first row
270271
* @param resource $link - Link identifier
271272
* @return array
272273
*/
273-
function fetchQueryToArray($sql, $link = 0) {
274-
$this->link = $link ? $link : $this->link;
274+
function fetchQueryToArray($sql, $fetchFirst = FALSE, $link = 0) {
275+
$this->link = $link ? $link : $this->link;
276+
if ($fetchFirst) {
277+
$sql = rtrim(trim($sql), ';');
278+
$sql = preg_replace('/limit(([\s]+([\d]+)[\s]*,[\s]*([\d]+))|([\s]+([\d]+)))$/i', 'LIMIT 1;', $sql);
279+
if (substr($sql, -strlen('LIMIT 1;')) !== 'LIMIT 1;') {
280+
$sql .= ' LIMIT 1;';
281+
}
282+
}
275283
$q = $this->query($sql, $this->link);
276284
$array = array();
277-
while ($row = $this->fetchArray($q)) $array[] = $row;
285+
if ($fetchFirst && $this->affected > 0) {
286+
$array = $this->fetchArray($q);
287+
} else {
288+
while ($row = $this->fetchArray($q)) $array[] = $row;
289+
}
278290
$this->freeResult($q);
279291
return $array;
280292
}
@@ -306,16 +318,32 @@ function arrayToUpdate($table, $data, $where = NULL, $limit = 0, $link = 0) {
306318

307319
/** Creates an sql string from an associate array
308320
* @param string $table - Table name
309-
* @param array $data - Data array Eg. $data['column'] = 'val';
321+
* @param array $data - Data array Eg. array('column' => 'val') or multirows array(array('column' => 'val'), array('column' => 'val2'))
310322
* @param boolean $ingore - INSERT IGNORE (row won't actually be inserted if it results in a duplicate key)
311323
* @param string $duplicateupdate - ON DUPLICATE KEY UPDATE (The ON DUPLICATE KEY UPDATE clause can contain multiple column assignments, separated by commas.)
312324
* @param resource $link - link identifier
313325
* @return insert id or false
314326
*/
315327
function arrayToInsert($table, $data, $ignore = FALSE, $duplicateupdate = NULL, $link = 0) {
316328
$this->link = $link ? $link : $this->link;
317-
foreach ($data as &$val) $val = (in_array(strtolower($val), $this->reserved)) ? strtoupper($val) : "'" . $this->escape($val) . "'";
318-
return (!empty($data)) ? $this->query("INSERT" . ($ignore ? " IGNORE" : NULL) . " INTO `{$table}` ( `" . implode('`, `', array_keys($data)) . "` ) VALUES ( " . implode(', ', array_values($data)) . " )" . ($duplicateupdate ? " ON DUPLICATE KEY UPDATE {$duplicateupdate}" : NULL) . ";") ? $this->insertId($this->link) : FALSE : FALSE;
329+
$depth = create_function('$a,$callback', '$m = 1; foreach ($a as $v) if (is_array($v)) { $d = $callback($v,$callback) + 1; if ($d > $m) $m = $d; } return $m;');
330+
$multirow = ($depth($data, $depth) == 2);
331+
if ($multirow) {
332+
$c = implode('`, `', array_keys($data[0]));
333+
$dat = array();
334+
foreach ($data as &$val) {
335+
foreach ($val as &$v) {
336+
$v = (in_array(strtolower($v), $this->reserved)) ? strtoupper($v) : "'" . $this->escape($v) . "'";
337+
}
338+
$dat[] = "( " . implode(', ', $val) . " )";
339+
}
340+
$v = implode(', ', $dat);
341+
} else {
342+
$c = implode('`, `', array_keys($data));
343+
foreach ($data as &$val) $val = (in_array(strtolower($val), $this->reserved)) ? strtoupper($val) : "'" . $this->escape($val) . "'";
344+
$v = "( " . implode(', ', $data) . " )";
345+
}
346+
return (!empty($data)) ? $this->query("INSERT" . ($ignore ? " IGNORE" : NULL) . " INTO `{$table}` ( `{$c}` ) VALUES {$v}" . ($duplicateupdate ? " ON DUPLICATE KEY UPDATE {$duplicateupdate}" : NULL) . ";") ? ($multirow ? TRUE : $this->insertId($this->link)) : FALSE : FALSE;
319347
}
320348

321349
/** Imports CSV data to Table with possibility to update rows while import.

example.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
define('DB', 'test');
1818

1919

20-
2120
// create test table
2221
$db = new MySQL_wrapper(HOST, USER, PASS, DB);
2322
$db->connect();
@@ -138,6 +137,9 @@
138137
$db->connect();
139138
echo "<hr /><strong>Example 5 (fetch query to array)</strong><pre>";
140139
print_r($db->fetchQueryToArray('SELECT * FROM `table`'));
140+
141+
// Returns only first row
142+
print_r($db->fetchQueryToArray('SELECT * FROM `table`', TRUE));
141143
echo "</pre>";
142144
$db->close();
143145
///////////////////////////////////////////////////////////////////////////////////////////
@@ -236,9 +238,27 @@
236238

237239
// $db->arrayToInsert( ... ) returns insert id
238240
$insert_id = $db->arrayToInsert('table', $data);
239-
$insert_id = $db->arrayToInsert('table', $data);
240241
echo "<hr /><strong>Example 9 (array to insert)</strong><br />Last insert id is: {$insert_id}<br />";
241242

243+
// Array data
244+
// [fealdname] = feald value
245+
$data = array();
246+
$data['firstname'] = 'Radovan';
247+
$data['surname'] = 'Janjic';
248+
$data['email'] = 'rade@it-radionica.com';
249+
$data['date'] = 'now()';
250+
251+
// [fealdname] = feald value
252+
$data2 = array();
253+
$data2['firstname'] = 'Radovan';
254+
$data2['surname'] = 'Janjic';
255+
$data2['email'] = 'rade@it-radionica.com';
256+
$data2['date'] = 'now()';
257+
258+
// $db->arrayToInsert( ... ) multirow returns TRUE on success
259+
$db->arrayToInsert('table', array($data, $data2 /*, $data3 .... */ ));
260+
261+
242262
// More options
243263
/** Creates an sql string from an associate array
244264
* @param string $table - Table name

0 commit comments

Comments
 (0)