3

使用 PDO 比较乏味的地方之一是它说缺少一些变量

PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: 绑定变量数与标记数不匹配

...但不告诉哪些。有没有办法识别它们?例如

$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"

很明显,每个人都需要这样的东西。但我不能一个简单的解决方案。

4

4 回答 4

2

由于我的声誉较低,我无法直接发表评论。因此,将其想象为对 2013 年 1 月 19 日 15:58 发布的 Ragen Dazs 的回复。我知道这个话题已经有几天了,但如果像我这样的人通过谷歌搜索偶然发现这个......

但是,我在倒数第三行中遇到了正则表达式的问题。正如您在此示例中看到的那样,表达式还匹配以 00:00:00 作为时间的时间值。所以我建议使用这个例子中的正则表达式。

我也想知道是否有不必要的参数。我就是这样做的,它需要一个类似于上面示例中的 SQL 查询和一个参数数组(参见 php doc example #2 for PDOStatement::execute)。

    /**
 * 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;
}

如果有人希望它在出现问题时抛出异常,只需替换“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);

玩得开心。

于 2017-01-20T10:34:59.960 回答
1

我想说命名​​占位符是 PDO 团队专门为此目的发明的 - 以简化查询和占位符的视觉验证。

我不得不同意,这样的功能可能是某种语法错误糖,但老实说,我觉得它不太有用。还有其他一些更令人费解的错误(比如you have an error near ''一个),而找到丢失的占位符并不是什么大问题。

于 2013-01-16T13:01:47.137 回答
1

经过几天搜索这个主题,测试了一些东西,我找到了这个解决方案 - 按照bind我的Database类的方法中的代码:

// [Method from Class Database]

/**
 * @param PDOStatement stmt
 * @param array data
 * @throws Exception if some variables are missing when interpolate query
 * @example $db->bind($stmt, $_POST)
 */
function bind(&$stmt, $data) {
    $sql = $stmt->queryString;
    // Sort $data keys to prevent erroneus bind parameters
    ksort($data);
    $data = array_reverse($data);
    foreach ($data as $_key => $_val) {
        if (preg_match("/:$_key/", $sql)) {
            $stmt->bindParam(":$_key", $data[$_key]);
            $sql = preg_replace("/:$_key/", $this->dbh->quote($data[$_key]), $sql, 1);
        }
    }

    //  if ($this->debug) {
    //      $this->fb->info($sql, 'SQL');
    //      $this->fb->info($stmt, 'Statment');
    //      $this->fb->info($data, 'Raw Data');
    //  }

    if (strpos($sql, ":")) {
        $matches = array();
        $this->dbg = preg_match_all('/:[A-Za-z0-9_]*/', $sql, $matches);
        throw new Exception('PDO Missing variables: ' . implode(', ', $matches[0]));
    }
}

此代码需要修改,如果有人发现错误或改进了某些东西,请分享!

于 2013-01-19T15:58:23.007 回答
0

更新的答案

第一次误读了这个问题,我现在可以理解你想要什么。据我所知,这是不可能的,因为错误消息是由 PDO 异常定义的。

但是,一种解决方法可能是为 PDO 编写一个包装类,并检查绑定值并自行查询,然后如果缺少值,则抛出您自己的异常。这可能是实现这一目标的最佳方式......

原始答案

很明显,每个人都需要这样的东西。但我不能一个简单的解决方案。

我不同意这一点,添加这种功能将毫无意义。考虑一下:

"SELECT id, name WHERE id = '1' AND name like  "

如果允许,这就是您的代码将输出的内容!显然,上述内容行不通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 ";
    }
}
于 2013-01-16T12:12:49.587 回答