@ 2014-07-23T00:02:02Z <?php
class SlightlyLessBollocksMySQLi extends mysqli
{
public function prepareWithParams($query, $params)
{
$tokens = $this->getQueryTokens($query);
$query = '';
$args = [''];
$paramRefs = [];
$i = 1;
foreach ($tokens as $token) {
if ($token !== '?') {
$query .= $token;
continue;
}
$paramRefs[$i] = array_shift($params);
switch (true) {
case is_array($paramRefs[$i]):
$query .= implode(', ', array_fill(0, count($paramRefs[$i]), '?'));
array_splice($paramRefs, $i, 1, $paramRefs[$i]);
for ($j = $i; isset($paramRefs[$j]); $j++) {
switch (true) {
case is_int($paramRefs[$j]):
case is_bool($paramRefs[$j]):
$args[0] .= 'i';
$paramRefs[$j] = (int)$paramRefs[$j];
break;
case is_float($paramRefs[$j]):
$args[0] .= 'd';
break;
default:
$args[0] .= 's';
$paramRefs[$j] = (string)$paramRefs[$j];
break;
}
$args[] = &$paramRefs[$j];
}
$i = $j;
break;
case is_int($paramRefs[$i]):
case is_bool($paramRefs[$i]):
$args[0] .= 'i';
$query .= '?';
$paramRefs[$i] = (int)$paramRefs[$i];
$args[] = &$paramRefs[$i++];
break;
case is_float($paramRefs[$i]):
$args[0] .= 'd';
$query .= '?';
$args[] = &$paramRefs[$i++];
break;
default:
$args[0] .= 's';
$query .= '?';
$paramRefs[$i] = (string)$paramRefs[$i];
$args[] = &$paramRefs[$i++];
break;
}
}
$stmt = $this->prepare($query);
call_user_func_array([$stmt, 'bind_param'], $args);
return $stmt;
}
/**
* @param string $token
* @return bool
*/
private function isQuotedString($token)
{
return in_array($token[0], array('"', "'")) && $token[0] == $token[strlen($token) - 1];
}
/**
* "Tokenize" the query
*
* Actually all this does is split the query up into things that are and are not quoted strings, to avoid treating
* a literal question mark as a placeholder.
*
* @param string $query
* @return string[]
*/
private function getQueryTokens($query)
{
static $expr = '/("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/';
$tokens = [];
foreach (preg_split($expr, $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) {
if ($this->isQuotedString($token)) {
$tokens[] = $token;
} else {
$tokens = array_merge($tokens, preg_split('/(\?)/', $token, -1, PREG_SPLIT_DELIM_CAPTURE));
}
}
return $tokens;
}
}
(new SlightlyLessBollocksMySQLi)->prepareWithParams("SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?)", [2, [1, 2, 12.345, "hello", true]]);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Fatal error: Uncaught Error: Class "mysqli" not found in /in/AQ5br:3
Stack trace:
#0 {main}
thrown in /in/AQ5br on line 3
Process exited with code 255 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
40.05 ms | 401 KiB | 8 Q