PDO PHP错误 - 参数号无效

时间:2013-02-11 03:11:01

标签: php pdo

我收到以下错误。有人在我的代码中看到了这个问题吗?感谢。

警告:PDOStatement :: execute()[pdostatement.execute]:SQLSTATE [HY093]:参数号无效:参数未在(我的文件)第309行(执行行)中定义

注意:名称中带“array”的变量不是实际数组。

$STH = $DBH->prepare("select jp.jobpost_id,jp.zipcode FROM jobposts jp JOIN zipcodes zc ON jp.zipcode=zc.zipcode JOIN employers em ON jp.employerid=em.employerid WHERE MATCH (jp.description) AGAINST (:keyword IN BOOLEAN MODE) AND
                                   jp.empstage LIKE :active
                               AND jp.title LIKE :title
                               AND jp.level >= :minlevel
                               AND jp.level <= :maxlevel
                               AND jp.experience >= :minexperience
                               AND jp.experience <= :maxexperience
                               AND jp.travel >= :mintravel
                               AND jp.travel <= :maxtravel
                               AND jp.education >= :mineducation
                               AND jp.education <= :maxeducation
                               AND jp.clearance >= :minclearance
                               AND jp.clearance <= :maxclearance
                               AND jp.hiredate >= :minhiredate
                               AND jp.hiredate <= :maxhiredate
                               AND jp.startdate >= :minstartdate
                               AND jp.startdate <= :maxstartdate
                               AND jp.probhire >= :minprobhire
                               AND jp.probhire <= :maxprobhire
                               AND jp.probstaff >= :minprobstaff
                               AND jp.probstaff <= :maxprobstaff
                               AND jp.salaryhigh >= :minsalaryhigh
                               AND jp.salaryhigh <= :maxsalaryhigh
                               AND jp.hourlyhigh >= :minhourlyhigh
                               AND jp.hourlyhigh <= :maxhourlyhigh
                               AND jp.salaryfee >= :minsalaryfee
                               AND jp.salaryfee <= :maxsalaryfee
                               AND jp.hourlyfee >= :minhourlyfee
                               AND jp.hourlyfee <= :maxhourlyfee
                               AND jp.shortcontractor LIKE :array_shortcontractor
                               AND jp.longcontractor LIKE :array_longcontractor
                               AND jp.tempperm LIKE :array_tempperm
                               AND jp.permplacement LIKE :array_permplacement
                               AND jp.open LIKE :like
                               AND em.staffingco NOT LIKE :staffingco
                               ");

                                ////// get results not factoring in zip code
                                $STH->bindValue(':keyword', $keyword);
                                $STH->bindValue(':active', 'active');
                                $STH->bindValue(':title', $title);
                                $STH->bindValue(':minlevel', $minlevel);
                                $STH->bindValue(':maxlevel', $maxlevel);
                                $STH->bindValue(':minexperience', $minexperience);
                                $STH->bindValue(':maxexperience', $maxexperience);
                                $STH->bindValue(':mintravel', $mintravel);
                                $STH->bindValue(':maxtravel', $maxtravel);
                                $STH->bindValue(':mineducation', $mineducation);
                                $STH->bindValue(':maxeducation', $maxeducation);
                                $STH->bindValue(':minclearance', $minclearance);
                                $STH->bindValue(':maxclearance', $maxclearance);
                                $STH->bindValue(':minhiredate', $minhiredate);
                                $STH->bindValue(':maxhiredate', $maxhiredate);
                                $STH->bindValue(':minstartdate', $minstartdate);
                                $STH->bindValue(':maxstartdate', $maxstartdate);
                                $STH->bindValue(':minprobhire', $minprobhire);
                                $STH->bindValue(':maxprobhire', $maxprobhire);
                                $STH->bindValue(':minprobstaff', $minprobstaff);
                                $STH->bindValue(':maxprobstaff', $maxprobstaff);
                                $STH->bindValue(':minsalaryhigh', $minsalaryhigh);
                                $STH->bindValue(':maxsalaryhigh', $maxsalaryhigh);
                                $STH->bindValue(':minhourlyhigh', $minhourlyhigh);
                                $STH->bindValue(':maxhourlyhigh', $maxhourlyhigh);
                                $STH->bindValue(':minsalaryfee', $minsalaryfee);
                                $STH->bindValue(':maxsalaryfee', $maxsalaryfee);
                                $STH->bindValue(':minhourlyfee', $minhourlyfee);
                                $STH->bindValue(':maxhourlyfee', $maxhourlyfee);
                                $STH->bindValue(':array_shortcontractor', $array_shortcontractor);
                                $STH->bindValue(':array_longcontractor', $array_longcontractor);
                                $STH->bindValue(':array_tempperm', $array_tempperm);
                                $STH->bindValue(':array_permplacement', $array_permplacement);
                                $STH->bindValue(':open', $open);
                                $STH->bindValue(':staffingco', $staffingco);
                                $STH->execute();

1 个答案:

答案 0 :(得分:2)

这是MySQL的奇怪特性,但您不能在MATCH() AGAINST()的参数中使用查询参数占位符。

您必须在prepare()之前插入字符串,因此它是SQL查询中的字符串文字。请务必使用PDO::quote()以确保正确转义特殊字符。

示例:

$keyword_quoted = $DBH->quote($keyword);

$STH = $DBH->prepare("select jp.jobpost_id,jp.zipcode 
  FROM jobposts jp 
  JOIN zipcodes zc ON jp.zipcode=zc.zipcode 
  JOIN employers em ON jp.employerid=em.employerid 
  WHERE MATCH (jp.description) AGAINST ($keyword_quoted IN BOOLEAN MODE) AND
  . . .

当然,您可以跳过bindValue($keyword)