2012-07-17 56 views
0

下面是一个功能,旨在处理自定义类的搜索方案。我的PDO绑定如何仍然搞乱我的查询?

我已经绊倒了PDO默认绑定参数为字符串的事实,即使它不合适也会导致整数 - >字符串转换。正如你将会看到的那样,我通过手动检查类型是否是整数,然后在这种情况下强制使用int来纠正这种情况。问题是,我的解决方案只适用于'开始'值为0 - 任何更高的错误,我不知道为什么。如果我手动将开始/计数值设置为其适当的值(即,而不是: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; 
    } 

回答

0

元数据(例如LIMIT参数)不能被参数化。您将不得不使用(正确消毒)插值。

+0

..解释为什么在这个问题上有这么多的线程。那么,至少真的很容易清理数据,应该是一个整数... – RonLugge 2012-07-17 05:00:43