首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警告: PDOStatement::execute() [pdostatement.execute]:SQLSTATE[HY093]:无效参数编号:

警告: PDOStatement::execute() [pdostatement.execute]:SQLSTATE[HY093]:无效参数编号:
EN

Stack Overflow用户
提问于 2012-06-13 12:01:18
回答 1查看 5.4K关注 0票数 0

我试图使用以下代码使用搜索字段中的单词搜索数据库。

由于某种原因,我得到了以下错误

警告: PDOStatement::bindValue() pdostatement.bindvalue: SQLSTATEHY093:无效参数编号:列/参数基于1

以下是函数代码:

代码语言:javascript
复制
// Retrieve search results
    function retrieve_search_posts($searchfield){
        //test the connection
        try{
            //connect to the database
            $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
        //if there is an error catch it here
        } catch( PDOException $e ) {
            //display the error
            echo $e->getMessage();

        }

        $where = array();

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

        $total_words = count($searchfield);

        for($i = 0; $i < count($words); $i++){

            $where[] .= "`post_title` LIKE ?";

        }

        $where_string = implode(" OR ", $where);

        $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 post_title LIKE ?
                                ORDER BY post_date
                                DESC";

        $stmt = $dbh->prepare($query);

        foreach($words AS $index => $word){
            $stmt->bindValue($index, $word, PDO::PARAM_STR);
        }

        $stmt->execute();

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

        return $searcharray;
    }

我做错了什么导致输出错误消息?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-13 12:11:13

首先,您没有使用在循环中精心构造的...OR LIKE子句字符串。

其次,正如错误所述,准备好的语句参数是1索引的,而数组是0索引的。您需要将数组中的所有索引向上移动1,以便使其与当前的foreach一起工作,或者在循环期间将1添加到其中。

试一试:

代码语言:javascript
复制
function retrieve_search_posts($searchfield){
    //test the connection
    try{
        //connect to the database
        $dbh = new PDO("mysql:host=localhost;dbname=mjbox","root", "usbw");
    //if there is an error catch it here
    } catch( PDOException $e ) {
        //display the error
        echo $e->getMessage();

    }

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

    // Easy way to 1-index a 0-indexed array
    array_unshift($words, '');
    unset($words[0]);

    // Never used and meaningless - $searchfield is a string
    // $total_words = count($searchfield);

    // Tidier and more resilient than the loop
    $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
       DESC
    ";

    $stmt = $dbh->prepare($query);

    foreach ($words AS $index => $word){
        // You may want to use "%$word%" or similar below, as it is the LIKE
        // keyword in your query is doing nothing and you might as well use =
        $stmt->bindValue($index, $word, PDO::PARAM_STR);
    }

    // Handle the potential error here!
    $stmt->execute();

    return $stmt->fetchAll(PDO::FETCH_ASSOC);

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11014405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档