1

So I'm building an SQL statement dynamically for a small search form.

I have a series of IFs where I check what filters the user decided to use when searching.

If the user decided to use the Operating System filter (a dropdown box he chooses from), I want to add

$statement->execute(array(
        ':osFilter'=>$osFilter,
        ));

If the user selected a second filter, I need another element to be added to this array so that it looks like this:

$statement->execute(array(
        ':osFilter'=>$osFilter,
        ':versionFilter'=>$versionFilter,
        ));

I thought the way to do this is to create an array as I go to through the IFs and then just pass it in execute() but it didn't work.

That's the code I tried:

 $filtersArray = array();

if (some condition) {
            array_push($filtersArray, array(
':osFilter' => $osFilter,
));
               }

I do the same for the rest of the IF statements and then pass the constructed array like this:

    $statement->execute($filtersArray);

I get this error: "Array to string conversion..."

Any ideas, what's the proper way of doing things here?

1 Answer 1

2

You create multi dimensional array. Just use $filtersArray[':osFilter'] = $osFilter;.

Sign up to request clarification or add additional context in comments.

1 Comment

I found the answer a few minutes ago but since you posted it first, I will accept. What a simple mistake I made but I never thought using push() will not work. Thanks anyway!

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.