0

我正在尝试使用以下代码在搜索字段中使用单词来搜索数据库。

出于某种原因,我收到以下错误

警告:PDOStatement::bindValue() [pdostatement.bindvalue]: SQLSTATE[HY093]: 参数号无效:列/参数从 1 开始。

这是功能代码:

// 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;
    }

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

4

2 回答 2

1

首先,您没有使用...OR LIKE您在循环中仔细构建的子句字符串。

其次,正如错误所说,准备好的语句参数是 1-indexed,而您的数组是 0-indexed。您需要将数组中的所有索引向上移动 1 以使其与您的 current 一起使用foreach,或者在循环期间向它们添加 1。

试试这个:

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);

}
于 2012-06-13T12:11:13.940 回答
1

“1-based”不是一个非常友好的错误信息

维基百科:http ://en.wikipedia.org/wiki/Array_data_type#Index_origin

基于 1 = 最小值为 1,因此 0(php/c 数组中的第一个值)无效

    foreach($words AS $index => $word){
        $stmt->bindValue($index+1, $word, PDO::PARAM_STR);
    }
于 2012-06-13T12:16:48.027 回答