<?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]), '?'));
$tmp = $paramRefs[$i];
array_splice($paramRefs, $i, 1, $tmp);
foreach ($tmp as $val) {
switch (true) {
case is_int($val): $args[0] .= 'i'; break;
case is_float($val): $args[0] .= 'd'; break;
default: $args[0] .= 's'; break;
}
}
for ($j = $i; isset($paramRefs[$j]); $j++) {
$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[$i] = &$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(?)", [2, [1, 2, 12.345, "hello"]]);
- Output for git.master, git.master_jit, rfc.property-hooks
- string(66) "SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?, ?, ?, ?)"
array(6) {
[0]=>
string(5) "iiids"
[1]=>
&int(2)
[2]=>
&int(1)
[3]=>
&int(2)
[4]=>
&float(12.345)
[5]=>
&string(5) "hello"
}
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:
50.98 ms | 401 KiB | 8 Q