几年来,我一直很高兴使用PDO debugger并高度推荐。
我没有找到更好的方法来帮助我调试带有绑定参数的PDO SQL语句。它将向我展示在替换绑定参数后生成的SQL语句,这是一件很好的事情。
我发布以下代码。我的问题是,当我使用SELECT
引入LIKE %gail.com
查询时,$ agencyEmailDomain ==" gmail.com"和$expandedSqlCommand = PdoDebugger::show($sqlText, $parameters);
扩展到
SELECT count(*) AS count FROM recruiters WHERE recruiter_email LIKE "%'Gmail.com'"
请注意域名周围的单引号?
我能做些什么,比如逃避%号?
我的代码是
$sqlText = 'SELECT count(*) AS count FROM recruiters
WHERE recruiter_email LIKE "%:agencyEmailDomain"';
$sqlParameters = array('agencyEmailDomain' => $agencyEmailDomain);
和PDO-debugger的代码是
/**
* Class PdoDebugger
*
* Emulates the PDO SQL statement in an extremely simple kind of way
*/
class PdoDebugger
{
/**
* Returns the emulated SQL string
*
* @param $raw_sql
* @param $parameters
* @return mixed
*/
static public function show($raw_sql, $parameters)
{
$keys = array();
$values = array();
/*
* Get longest keys first, so that the regex replacement doesn't
* cut markers (ex : replace ":username" with "'joe'name"
* if we have a param name :user )
*/
$isNamedMarkers = false;
if (count($parameters) && is_string(key($parameters)))
{
uksort($parameters, function($k1, $k2)
{
return strlen($k2) - strlen($k1);
});
$isNamedMarkers = true;
}
foreach ($parameters as $key => $value)
{
// check if named parameters (':param') or anonymous parameters ('?') are used
if (is_string($key))
{
$keys[] = '/:'.ltrim($key, ':').'/';
}
else
{
$keys[] = '/[?]/';
}
// bring parameter into human-readable format
if (is_string($value))
{
$values[] = "'" . addslashes($value) . "'";
}
elseif(is_int($value))
{
$values[] = strval($value);
}
elseif (is_float($value))
{
$values[] = strval($value);
}
elseif (is_array($value))
{
$values[] = implode(',', $value);
}
elseif (is_null($value))
{
$values[] = 'NULL';
}
}
if ($isNamedMarkers)
{
return preg_replace($keys, $values, $raw_sql);
}
else
{
return preg_replace($keys, $values, $raw_sql, 1, $count);
}
}
}
如何更改以正确处理LIKE "%:agencyEmailDomain"
?