Chris
Chris

Reputation: 145

php implode question

So I am doing a search and I am using an implode in my select statement, which I find quite useful. Basically this search engine will have 3 different selects which will select different things based on different criteria and when I use my implode I get an error of invalid arguments passed.

Here is my sql statement:

$sql = "SELECT DISTINCT camp.title, camp.startDay, camp.typeOfCamp, camp.endDay,
camp.link FROM ((camp INNER JOIN gender ON camp.id = gender.camp_id) 
INNER JOIN grades ON camp.id = grades.camp_id) 
INNER JOIN interests ON camp.id = interests.camp_id 
WHERE ((grades.year = '".implode('\' OR grades.year = \'',$age)."') 
AND gender.gender = '".$gender."') 
OR ((interests.activity = '".implode('\' OR interests.activity = \'',$array)."')
AND (grades.year = '".$age."' AND gender.gender = '".$gender."'))";

The second implode for the interests is where I began having my problem and $array is an array. Another thing I don't understand is that when I run my code I get the correct results, but I am still getting the error that I am passing invalid arguments.

Upvotes: 0

Views: 263

Answers (4)

Stephen Perelson
Stephen Perelson

Reputation: 6773

Since you are using strings:

OR (interests.activity IN ('".implode("','", $array)."')

will neaten up that line.

But I am worried about $age. You use it as if it is an array in:

WHERE ((grades.year = '".implode('\' OR grades.year = \'',$age)."') 

but as a string variable:

AND (grades.year = '".$age."' AND gender.gender = '".$gender."'))";

I think you should be using the "IN" and implode() expressions for both places you're using $age if it is an array.

Upvotes: 0

NikiC
NikiC

Reputation: 101936

You may believe what PHP says, normally. If it says it isn't an array you probably didn't pass an array.

And a small tip to save you some code: There is an IN() statement in MySQL:

$sql = "SELECT DISTINCT camp.title, camp.startDay, camp.typeOfCamp, camp.endDay,
camp.link FROM ((camp INNER JOIN gender ON camp.id = gender.camp_id) 
INNER JOIN grades ON camp.id = grades.camp_id) 
INNER JOIN interests ON camp.id = interests.camp_id 
WHERE (grades.year IN(".implode(',', $age).") 
AND gender.gender = '".$gender."') 
OR (interests.activity IN('".implode("','", $array)."')
AND grades.year = ".$age." AND gender.gender = '".$gender."')";

To save the duplicate gender.gender = $gender and (maybe) optimize the query:

$sql = "SELECT DISTINCT camp.title, camp.startDay, camp.typeOfCamp, camp.endDay,
camp.link FROM ((camp INNER JOIN gender ON camp.id = gender.camp_id) 
INNER JOIN grades ON camp.id = grades.camp_id) 
INNER JOIN interests ON camp.id = interests.camp_id 
WHERE gender.gender = '".$gender."'
AND (
    grades.year IN(".implode(',', $age).") 
    OR (
        interests.activity IN('".implode("','", $array)."')
        AND grades.year = ".$age."
    )
)";

Furthermore I think MySQL doesn't require to use all those parentheses for joins:

$sql =
"SELECT DISTINCT camp.title, camp.startDay, camp.typeOfCamp, camp.endDay,
camp.link
FROM camp
INNER JOIN gender    ON camp.id = gender.camp_id
INNER JOIN grades    ON camp.id = grades.camp_id
INNER JOIN interests ON camp.id = interests.camp_id 
WHERE gender.gender = '".$gender."'
AND (
    grades.year IN(".implode(',', $age).") 
    OR (
        interests.activity IN('".implode("','", $array)."')
        AND grades.year = ".$age."
    )
)";

Now the query should be way better to understand.

Upvotes: 2

Paul Dragoonis
Paul Dragoonis

Reputation: 2333

Chris, your problem is that you're not passing an array to the second parameter of implode().

If you see www.php.net/implode the second parameter must be of type ARRAY so that PHP can start joining your array together with your delimiter specified in the first parameter.

A good way to check the data type of your variable is to use var_dump() which will tell you if something is an array, or int..etc

There are two variables you're passing to implode() there. $age and $array, I am guessing that it's age and you need to modify your code a bit.

Pasting your whole code that's generating $array and $age would help us further to see where you're going wrong in creating those variables.

Hope this helps.

Upvotes: 0

Whit
Whit

Reputation: 1159

You're trying to do everything in on big statement. Why not build your imploded array-strings before you include them in the SQL?

Upvotes: 0

Related Questions