Last active
December 20, 2015 08:38
-
-
Save fenixkim/6101485 to your computer and use it in GitHub Desktop.
Kirby toolkit db::query to parse binary string to hexadecimal automatically using the bin2hex PHP function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Runs a MySQL query. | |
* You can use any valid MySQL query here. | |
* This is also the fallback method if you | |
* can't use one of the provided shortcut methods | |
* from this class. | |
* | |
* @param string $sql The sql query | |
* @param boolean $fetch True: apply db::fetch to the result, false: go without db::fetch | |
* @return mixed | |
*/ | |
static function query($sql, $fetch=true) { | |
$connection = self::connect(); | |
if(error($connection)) return $connection; | |
// save the query | |
self::$last_query = $sql; | |
// execute the query | |
$result = @mysql_query($sql, $connection); | |
self::$affected = @mysql_affected_rows(); | |
self::$trace[] = $sql; | |
if(!$result) return self::error(l::get('db.errors.query_failed', 'The database query failed')); | |
if(!$fetch) return $result; | |
$array = array(); | |
while($r = self::fetch($result)) { | |
// Parses binary strings | |
foreach ($r as $k => $v) $r[$k] = (is_string($v) && substr_count($v, "\x00") > 0) ? bin2hex($v) : $v; | |
array_push($array, $r); | |
} | |
return $array; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment