Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:58:05+00:00 2026-06-05T15:58:05+00:00

I am attempting to use the following code to search a database using words

  • 0

I am attempting to use the following code to search a database using words in a search field.

For some reason I am getting the following error

Warning: PDOStatement::bindValue() [pdostatement.bindvalue]:
SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are
1-based in..

Here is the function code:

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

What have I done wrong to cause the outputted error message?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:58:08+00:00Added an answer on June 5, 2026 at 3:58 pm

    Firstly, you didn’t use the string of ...OR LIKE clauses you carefully constructed in the loop.

    Secondly, as the error says, prepared statement parameters are 1-indexed, whereas your array is 0-indexed. You need to shift all the indexes in the array up 1 in order to get it to work with your current foreach, or add 1 to them during the loop.

    Try this instead:

    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);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the following error when attempting to use django-openid-auth OpenID discovery error: No
I am attempting to use a jquery drowndown checklist control. Following some simple examples,
I'm attempting to learn how to use Cucumber and creating steps using the following
I have the following Groovy code snippet that is attempting to use operator overloading
I am attempting to create the following code through the use of CodeDom: public
I'm having some issues using the following code on user input: htmlentities($string, ENT_COMPAT, 'UTF-8');
I'm attempting to use the following code to serialize an anonymous type to JSON:
I am attempting to use the following code to upload a photo to Facebook
Using the following code I am attempting to: Test to see if one of
I'm attempting to use the following sql to retrieve the total amount received, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.