// optionally `return false;` in callback to stop processing
function fetch_callback($sql, callable $callback)
{
$result = null;
$rs = $this->query($sql, MYSQLI_USE_RESULT);
while ($row = $rs->fetch_assoc())
{
// if (call_user_func_array($callback, [&$row, &$result]) === false) // alternative: when using references, the values MUST be passed via an array with call_user_func_array(), call_user_func() only passes by value!
if ($callback($row, $result) === false)
break;
}
$rs->free_result();
return $result;
}
function get_array($sql, $indexes = null, $values = null)
{
$rs = $this->query($sql, MYSQLI_USE_RESULT); // MYSQLI_USE_RESULT was about 12% faster than MYSQLI_STORE_RESULT
if (isset($indexes))
{
if ($indexes == '#') // used in the ajax queries where we need to `override` the two field array index => value (field_count === 2) below! eg. db::get_array($sql, '#', array('id', 'value'))
$tmp = $rs->fetch_all(MYSQLI_ASSOC); // MYSQLI_ASSOC || MYSQLI_NUM || MYSQLI_BOTH WARNING: This has caused memory issues before!!! Unable to allocate n bytes!!! `SELECT * FROM city_catchment` = 2,981,304 rows | `data length` = 31MB (will be even larger in PHP)
foreach ($tmp as $row) // Note: I tried using &$row here but it was slightly slower when just returning the recset without any processing on it, maybe it's faster if we do something with the recset?? Was 9.1s vs 9.5s!
$result[$row[$indexes]] = $row;
else // $indexes is an array
{
if (isset($values))
if (is_string($values))
foreach ($tmp as $row)
{
$entry = &$result;
foreach ($indexes as $index)
$entry = &$entry[$row[$index]];
$entry = $row[$values];
}
else // $values is an array
{
$values = array_flip($values);
foreach ($tmp as $row)
{
$entry = &$result;
foreach ($indexes as $index)
$entry = &$entry[$row[$index]];
$entry = array_intersect_key($row, $values);
}
}
else // $values is null
foreach ($tmp as $row)
{
$entry = &$result;
foreach ($indexes as $index)
$entry = &$entry[$row[$index]];
$entry = $row;
}
}
}
}
else if ($rs->field_count === 2) // NOTE: We can construct this type of array from a larger (superset) array with the make_select_array() static HELPER function!
while ($this->more_results() && $this->next_result()); // Added on 2 May 2013; To be compatible with Stored Procedures (CALL ...)
return $result;
}
function lookup($sql)
{
$rs = $this->query($sql, MYSQLI_USE_RESULT);
if ($rs->field_count == 1)
{
$row = $rs->fetch_row();
$rs->free_result();
return $row[0];
}
$row = $rs->fetch_assoc();
$rs->free_result();
return $row;
}
function call($sql) // the original `lookup` code ... the `more_results` section is the only one required by stored procedures!
{
//$this->real_query('CALL ' . $sql); // not sure if I want to do this ??? will have to concat the string, but call('CALL ...') looks funny with 2x call's!
$rs = $this->query($sql, MYSQLI_USE_RESULT);
if ($rs->field_count == 1)
{
$row = $rs->fetch_row();
$rs->free_result();
while ($this->more_results() && $this->next_result()); // Added on 23 Feb 2013; To be compatible with Stored Procedures (CALL ...)
return $row[0];
}
$row = $rs->fetch_assoc();
$rs->free_result();
while ($this->more_results() && $this->next_result()); // Added on 23 Feb 2013; To be compatible with Stored Procedures (CALL ...)
return $row;
}
// MySQL only accepts 3-byte UTF-8! SANITIZE our "UTF-8" string for MySQL!
// Taken from: http://stackoverflow.com/questions/8491431/remove-4-byte-characters-from-a-utf-8-string
function escape($value, $prefix = false) // AKA sanitize string! My NEW version which includes UTF-8 cleaning as well! I DO NOT KNOW WHERE THIS FUNCTION `escape` was being used ... so I `replaced` it on 22 July 2015 with my enhanced version `new_escape` which was built out of trying to replace the NULLable parameters ...
{ // CODE FROM new_esacpe() written on 22 July 2015 @ 5pm (about) ... updated and re-written on 11 Dec 2015!
if (is_numeric($value)) return ($prefix?'= ':null) . $value;
// This function is used in post.php files to remove 4-byte UTF-8 characters (MySQL only accepts upto 3-bytes), pack multiple space values, trim and get only $length characters!
$str = trim(mb_substr(trim(mb_ereg_replace($compact ? '\s+' : ' +', ' ', self::utf8($str))), 0, $length)); // 2x trim() because after shortening it, we could have a space at the end of our shortened (substr) string
if ( ! isset($curdate)) $curdate = date('Y-m-d'); // `fast` PHP server date version!
return $curdate;
/*
// taken from some older `request.php` init() code, but I don't think should be in there! This is a database lookup of the DATABASE server `curdate` which should be `more accurate` but slower!
$tmp = db::lookup('SELECT UNIX_TIMESTAMP() AS timestamp, CURDATE() AS curdate');
self::$timestamp = $tmp['timestamp'];
self::$curdate = $tmp['curdate']; // database `slower` version