www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Why this error: Invalid parameter number: number of bound variables does not match nu

  1. #1
    Join Date
    Feb 2013
    Posts
    7

    resolved [RESOLVED] Why this error: Invalid parameter number: number of bound variables does not match nu

    Hi all,

    I'm trying to create a prepared dynamic select statement using PDO, and it appears to me that I've got the right number of variables to parameters.

    The input:

    $var1 is not null
    $var2 is not null
    $var3 is not null
    $var4 is not null
    $var5 is null


    The code:

    PHP Code:
    //**** Begin building the query statement. ****//
    $buildSTMT "SELECT * FROM Table1 WHERE ";
    $bindArray = array();  //**** Create an empty array to add to, for the execute statement, when variables are found.

    if ($var1 != "") {
        
    $buildSTMT .= "Col1 = ? ";
        
    $bindArray[] = "$var1";
    }

    if (
    $var2 != "") {  
        
    $buildSTMT .= "AND Col2 = ?";
        
    $bindArray[] = "$var2";
    }

    if (
    $var3 != "") { 
        
    $buildSTMT .= " AND Col3 = ?";
        
    $bindArray[] = "$var3";
    }

    if (
    $var4 != "") {
        
    $buildSTMT .= " AND Col4 = ?";
        
    $bindArray[] = "$var4";
    }

    if (
    $var5 != "") {
        
    $buildSTMT .= " AND Col5 = ?";
        
    $bindArray[] = "$var5";
    }


    $stringOfParms implode (", "$bindArray);

    echo 
    "bind array count: ".count($bindArray)."<p>";  //output shows 4.
    echo "string: ".$stringOfParms."<p>";  //string echos 4 items separated by commas "string: apple, banana, carrot, dragonfruit"
    echo "built: ".$buildSTMT."<p>"//echos "built: SELECT * FROM Table1 WHERE Col1 = ? AND Col2 = ? AND Col3 = ? AND Col4 = ?

    $stmt $connection->prepare($buildSTMT);

    $stmt->execute(array($stringOfParms));  //This is line #118 the error is occuring on. 
    The error:

    Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in [blah] on line 118
    Why this error?

    b.t.w. I'm trying to replicate Example #3 here: http://www.php.net/manual/en/pdostatement.execute.php

    (which doesn't show a "bind" step).

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Just pass the $bindArray array into the execute. (What you are doing is converting it into a string, and then passing an array into the execute that only has one element: that comma-delimited string.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Feb 2013
    Posts
    7
    Thanks for the reply. I've changed the execute line to (without changing anything else):

    PHP Code:
    $stmt->execute(array($bindArray)); 
    And get the same exact error. Ideas why?

  4. #4
    Join Date
    May 2013
    Location
    London, UK
    Posts
    16
    Quote Originally Posted by we5inelgr View Post
    Thanks for the reply. I've changed the execute line to (without changing anything else):

    PHP Code:
    $stmt->execute(array($bindArray)); 
    And get the same exact error. Ideas why?
    You are creating an array with one element, the other array. Change to the following:

    PHP Code:
    $stmt->execute($bindArray); 

  5. #5
    Join Date
    Feb 2013
    Posts
    7
    oh geez. that works now. sorry for being so obtuse

    many thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles