@ 2014-07-23T00:23:36Z <?php
class SlightlyLessBollocksMySQLi
{
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;
}
}
var_dump($query, $args);
}
/**
* @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(?) OR yomama = ?", [2, [1, 2, 12.345, "hello", true], "gay"]);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.7 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 string(83) "SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?, ?, ?, ?, ?) OR yomama = ?"
array(8) {
[0]=>
string(7) "iiidsis"
[1]=>
&int(2)
[2]=>
&int(1)
[3]=>
&int(2)
[4]=>
&float(12.345)
[5]=>
&string(5) "hello"
[6]=>
&int(1)
[7]=>
&string(3) "gay"
}
Output for 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 , 5.3.0 - 5.3.29 Parse error: syntax error, unexpected '[' in /in/Kngnp on line 9
Process exited with code 255 . Output for 5.0.0 - 5.0.5 Parse error: parse error, unexpected '[' in /in/Kngnp on line 9
Process exited with code 255 . Output for 4.4.2 - 4.4.9 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Kngnp on line 5
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Kngnp on line 5
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Kngnp on line 5
Process exited with code 255 . preferences:dark mode live preview
209.05 ms | 401 KiB | 356 Q