我的PDO绑定怎么仍然扰乱了我的查询


How is my PDO binding still messing up my query?

下面是一个用于处理自定义类的搜索场景的函数。

我已经被PDO默认将参数绑定为字符串这一事实绊倒了,即使这不合适,也会导致integer->字符串转换。正如您所看到的,我通过手动检查类型是否为integer来纠正这一点,然后在这些情况下强制使用int。问题是,我的解决方案只适用于"开始"值为0的情况——任何更高的错误都会出现,我不知道为什么。如果我手动将start/count值设置为适当的值(即,我使用{$count}而不是:count),一切都很好,所以看起来绑定仍然很混乱。

如何?或者如果我错了。。。什么是正确的?

    /*Query is:  
    SELECT tutor_school.id 
    FROM tutor_school, tutor_states 
    WHERE tutor_states.stateName=:state AND tutor_states.id=tutor_school.state 
    GROUP BY tutor_school.id order by tutor_school.name asc 
    LIMIT :start, :count*/
    $db = Database::get_user_db();
    $statement = $db->prepare($query);
    foreach ($executeArray as $key => $value)
    {
        if (getType($value) == 'integer')
        {
            $statement->bindParam($key, $executeArray[$key], PDO::PARAM_INT);
        }
        else
        {
            $statement->bindParam($key, $value);
        }
    }
    var_dump($executeArray);//count and start are still ints
    if ($statement->execute())
    {
        var_dump($executeArray);//start and count are now strings
        var_dump($statement->errorInfo());
        var_dump($query);
        $values = $statement->fetchAll();
        $return = array();
        foreach ($values as $row)
        {
            $school = School::schoolWithId($row[0]);
            if (!empty($school))
            {
                $return[] = $school;
            }
        }
        return $return;
    }

元数据(如LIMIT参数)无法参数化。您将不得不使用(适当消毒)插值。