微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – PDO:参数号无效:混合命名和位置参数

我遇到过这个我以前没看过的警告:

Warning: PDOStatement::execute() [pdostatement.execute]: sqlSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in…

参考以下PDO查询(简化了易于阅读的功能):

$offset = 0;
$limit = 12;
function retrieve_search_posts($searchfield,$offset,$limit){


        $where = array();

        $words = preg_split('/[\s]+/',$searchfield);

        array_unshift($words,'');
        unset($words[0]);

        $where_string = implode(" OR ",array_fill(0,count($words),"`post_title` LIKE ?"));

        $query = "
                                SELECT  p.post_id,post_year,post_desc,post_title,post_date,img_file_name,p.cat_id
                                FROM    mjBox_posts p
                                JOIN    mjBox_images i
                                ON      i.post_id = p.post_id
                                        AND i.cat_id = p.cat_id
                                        AND i.img_is_thumb = 1
                                        AND post_active = 1
                                WHERE $where_string
                                ORDER BY post_date
                                LIMIT :offset,:limit
                                DESC";
        $stmt = $dbh->prepare($query);

        foreach($words AS $index => $word){
            $stmt->bindValue($index,"%".$word."%",PDO::ParaM_STR);
        }
        $stmt->bindParam(':offset',PDO::ParaM_INT);
        $stmt->bindParam(':limit',$limit,PDO::ParaM_INT);
        $stmt->execute();

        $searcharray = $stmt->fetchAll(PDO::FETCH_ASSOC);

        return $searcharray;
    }

函数和PDO查询工作正常,没有查询中包含的偏移量和限制变量.那么可能导致这个警告呢?

谢谢

更改
LIMIT :offset,:limit

LIMIT ?,?

$stmt->bindParam(':offset',PDO::ParaM_INT);
$stmt->bindParam(':limit',PDO::ParaM_INT);

至:

$stmt->bindValue($index+1,PDO::ParaM_INT);
$stmt->bindValue($index+2,PDO::ParaM_INT);

原文地址:https://www.jb51.cc/php/131833.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐