Php PDO识别缺失的绑定变量
使用PDO的一个更乏味的问题是,它表示缺少一些变量 PDOStatement::execute():SQLSTATE[HY093]:参数编号无效:绑定变量的编号与令牌的编号不匹配 。。。但不知道是哪一个。有什么办法可以识别它们吗?乙二醇Php PDO识别缺失的绑定变量,php,pdo,Php,Pdo,使用PDO的一个更乏味的问题是,它表示缺少一些变量 PDOStatement::execute():SQLSTATE[HY093]:参数编号无效:绑定变量的编号与令牌的编号不匹配 。。。但不知道是哪一个。有什么办法可以识别它们吗?乙二醇 $sql = "SELECT id, name WHERE id = :id AND name like :search_tem"; $stmt = $pdo->prepare($sql); $stmt->bindValue(':id', '1'
$sql = "SELECT id, name WHERE id = :id AND name like :search_tem";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':id', '1');
$stmt->execute(); // throws exception - "**search_term missing"
很明显每个人都需要这样的东西。但我无法找到一个简单的解决方案。更新答案
我第一次误读了这个问题,现在我明白你想要什么了。就我所见,这是不可能的,因为错误消息是由PDO异常定义的
然而,一种解决方法是为PDO编写一个包装类,检查绑定值并自己查询,然后如果缺少值,抛出自己的异常。这可能是实现这一目标的最佳方式
原始答案
很明显每个人都需要这样的东西。但我不能接受
简单的解决方案
我不同意这一点,添加这种功能是没有意义的。考虑这一点:
"SELECT id, name WHERE id = '1' AND name like "
如果允许的话,这就是您的代码将输出的内容!显然,上面的不起作用,因为中没有类似于术语的内容
我可以理解您为什么要这样做,因为我过去也忘记(有意无意地)绑定值,但是,实际上没有任何好处,因为如果您不绑定值,查询将无法工作
您最好像这样动态构建查询:
// Gather the where parameters
$get_data_where = array();
// Field 2
if(!empty($parameters['field_1'])){
$get_data_where[':field_1'] = '%'.$parameters['field_1'].'%';
}
// Field 2
if(!empty($parameters['field_2'])){
$get_data_where[':field_2'] = $parameters['field_2'];
}
// Combine the where array with the bind valus array
$this->mssql->bind_values = array_merge($this->mssql->bind_values,$get_data_where);
// Prepare the SQL to gather the data
$SQL = "SELECT * \n";
$SQL .= "FROM [Some_Table] AS ST \n";
// Append the where statement if necessary
// Build the where statement based on bind values
if(!empty($get_data_where)){
$SQL .= "WHERE \n\t";
// Field 1
if(!empty($this->mssql->bind_values[':field_1'])){
$SQL .= $where_and."ST.[Field_1] LIKE :field_1 \n\t";
// Separate the where conditions
$where_and = "AND ";
}
// Field 2
if(!empty($this->mssql->bind_values[':field_2'])){
$SQL .= $where_and."ST.[Field_2] = :field_2 \n\t";
// Separate the where conditions
$where_and = "AND ";
}
}
我想说,命名占位符是PDO团队专门为此目的发明的,目的是简化查询和占位符的视觉验证
我不得不同意,这样一个特性可能是某种语法错误,但老实说,我觉得它不太有用。还有其他更令人费解的错误(例如,您在“”附近有一个错误
一个),而查找丢失的占位符并不是什么大问题。经过几天的搜索,测试了一些我找到这个解决方案的东西后,请遵循我的数据库类中的绑定
方法中的代码:
这段代码需要修改,如果有人发现了一个bug或者改进了一些东西,请分享 因为我的名声不好,我不能直接评论。想象一下,这是对拉根·达斯(Ragen Dazs)于2013年1月19日15:58发布的帖子的回复。
我知道这个话题已经有几天了,但是如果像我这样的人在谷歌搜索中偶然发现这个话题
然而,最后第三行中的正则表达式有问题。如中所示,表达式还将时间值与00:00:00作为时间进行匹配。所以我建议使用这个正则表达式
我还想知道是否有不必要的参数。我就是这样做的,它需要一个像上面示例中那样的SQL查询和一个参数数组(参见php文档示例2)
如果有人希望它在出现问题时抛出异常,只需将“return$result;”替换为以下代码行:
$missingCount = 0;
$missing = "";
$needlessCount = 0;
$needless = "";
if( array_key_exists('missing', $parameters) )
{
$missingCount = count($parameters[ 'missing' ]);
$missing = " (" . implode(", ", $parameters[ 'missing' ]) . ") ";
}
if( array_key_exists('needless', $parameters) )
{
$needlessCount = count($parameters[ 'needless' ]);
$needless = " (" . implode(", ", $parameters[ 'needless' ]) . ")";
}
$msg = "There are " . $missingCount . " missing parameter(s)".$missing." and ".$needlessCount." needless parameter(s)".$needless.".";
throw new Exception($msg);
玩得开心。您需要绑定:搜索项目too@zan注意到显而易见的事情做得很好!重新阅读此问题,您会注意到此用户非常清楚这一点!你说这是一个毫无意义的特征,这表明你误解了这个问题。他不希望无效的操作成功,他希望错误消息(当前无效的参数编号:绑定变量的数量与标记的数量不匹配)告诉哪些是缺少的参数,这是非常合理的。@IMHO@lvaroG.Vicario你是对的,我错读了问题,不是很清楚!如果是那样的话,我会同意的,我会修改我的答案!这更令人费解,但找到缺失的变量是无聊和重复的。毕竟我们花了很多时间在这些小事情上。我发现这个表达还有另一个问题。如果使用日期\格式。因此,我将原始帖子中的表达式从“/:\\D\\w*/”更新为“/:[^%]\\D\\w*/”,并链接到这个新示例:
/**
* Checks an parameter array against the sql query. it will tell you if there are any missing or needless parameters
*
* @param string $query Sql query
* @param array $parameters Parameters array
*
* @return bool|array Returns TRUE if no missing or needless parameters where found or a list with the missing
* or needless parameters
*/
private function checkParameters($query, $parameters)
{
$parameterTMP = $parameters;
$parameterCount = count($parameterTMP);
$regexMatchCounter = preg_match_all("/:[^]\\D\\w*/", $query, $regexMatches);
// if there are parameter in the $parameters array oder parameters in the sql query
if( $parameterCount > 0 || $regexMatchCounter > 0 )
{
// take every parameter found in the sql query
foreach( $regexMatches[ 0 ] as $parameterName )
{
// check if the required parameter is in the parameters array
if( !array_key_exists($parameterName, $parameters) )
{
// if it is not in the parameters array, add it to the list of missing parameters
// and continue with the next parameter from the query
$result[ 'missing' ][] = $parameterName;
continue;
}
// if the required parameter is in the parameter array, delete it from this array
// so we get a list of parameters that are needless
unset($parameterTMP[ $parameterName ]);
}
// check if there are (needless) parameters left
if( count($parameterTMP) > 0 )
{
// if so, add them to the list of needles parameters
$result[ 'needless' ] = array_keys($parameterTMP);
}
// if at this point $result is an array,
// some parameters are missing or needless, so we return the result list(s)
if( isset($result) && is_array($result) )
{
return $result;
}
}
// if we reach this point, no missing or needless parameters where found,
// you are good to go
return true;
}
$missingCount = 0;
$missing = "";
$needlessCount = 0;
$needless = "";
if( array_key_exists('missing', $parameters) )
{
$missingCount = count($parameters[ 'missing' ]);
$missing = " (" . implode(", ", $parameters[ 'missing' ]) . ") ";
}
if( array_key_exists('needless', $parameters) )
{
$needlessCount = count($parameters[ 'needless' ]);
$needless = " (" . implode(", ", $parameters[ 'needless' ]) . ")";
}
$msg = "There are " . $missingCount . " missing parameter(s)".$missing." and ".$needlessCount." needless parameter(s)".$needless.".";
throw new Exception($msg);