0

I'm trying to add a search feature on my site there are 4 different inputs the user can use although they might not use all 4. I'm appending to my sql query depending on which inputs they fill in;

$query = "SELECT * FROM cars WHERE status = 2 ";
if($_GET['ref']){
  $query .= " AND ref = :ref";
}
if($_GET['doors']){
  $query .= " AND doors = :doors";
}
if($_GET['wheels']){
  $query .= " AND wheels = :wheels";
}
if($_GET['location']){
  $query .= " AND location = :location";
}
$query .= ")";

$adverts = Singlequery ($query, array(              
        'ref' => $_GET['ref'],
        'doors' => $_GET['doors'],
        'wheels' => $_GET['wheels'],
        'location' => $_GET['location']
), $conn);

This is my query I'm using -

function query($query, $bindings, $conn)
{
    $stmt = $conn->prepare($query);
    $stmt->execute($bindings);

    return $stmt;
}

I'm getting the error -

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

I think its expecting all 4 inputs to be used and therefore wants 4 bound variables.

3
  • Yes, you can use other IF (example) Commented Dec 18, 2014 at 11:36
  • 2
    try to echo $query so that you'll know what to expect Commented Dec 18, 2014 at 11:36
  • It's a verbose way to generate a query like this, just set the values of the array which used for binding in the if($_GET ..) statements too, e.g. $query .= "AND location ..."; $bind[':location'] = $_GET['location']; etc. Commented Dec 18, 2014 at 11:40

4 Answers 4

4

First of all you have a missing parenthesis in your query. You are closing it but not opening.

You should create your variable array also in if clauses as :

$query = "SELECT * FROM cars WHERE status = 2 ";
$data=array();
if($_GET['ref']){
  $query .= " AND ref = :ref";
  $data['ref']=$_GET['ref'];
}
if($_GET['doors']){
  $query .= " AND doors = :doors";
  $data['doors']=$_GET['doors'];
}
if($_GET['wheels']){
  $query .= " AND wheels = :wheels";
  $data['wheels']=$_GET['wheels'];
}
if($_GET['location']){
  $query .= " AND location = :location";
  $data['location']=$_GET['location'];
}

$adverts = Singlequery($query, $data, $conn);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I see what you've done and how you've done it big help thanks!
0

Try to use ? instead the :alias format.

Comments

0

Maybe you can try to say "if this variable is empty, set it to Null" Then the variables that stay empty will be Null instead of undefined.

Comments

0

Could it be because you always feed it an array of size four as to bind, while your query string sometimes won't have as many due to the if statements, hence the error message?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.