tags:

views:

28

answers:

3

When the user check more than one (checkbox) option which are then combine into a string of "apple,orange,pear"

SELECT id, pos, FROM $db WHERE dtime>='$now' AND jsub IN ('$arr[1]') ;

When I pass the string to $arr[1], it won't work correctly, how do I split into array and get mysql IN function to process correctly?

+1  A: 

use:

    $str = "SELECT id, pos, FROM $db
            WHERE dtime>='$now' AND jsub IN ('".explode(',',$arr."')";

and don't forget to sanitize the parameters before ...

aviv
There was a missing bracket, I fix it... However, it doesn't seen to work in my case, nevermind I manage to solve using replace string to add a ' on both side of the comma.$arr[1] = str_replace(",", "','", $arr[1]);
proyb2
A: 

the question is WAY unclear, but I suspect you want something like

foreach ($arr as $key => $item) $arr[$key] = mysql_real_escape_string($item);
$in = "'".implode("','",$arr);
$sql = "SELECT id, pos, FROM $db WHERE dtime>='$now' AND jsub IN ($in)";

But man, I hate guessing.

Why don't you get yourself a static query, without any PHP code?
To see, if it ever works?
If not - ask on SO for the proper query. SQL query, not PHP code.
If yes - write a PHP code that produces the exact query.
Compare to the example one.
If failed - ask on SO for the PHP code, providing an example of the resulting query and an array.

Is it too hard rules to follow?

Col. Shrapnel
nevermind, I solve it. Doesn't need to implode.
proyb2
I suspected something of the kind. Lack of debugging, as usual. Home my simple checklist'll help
Col. Shrapnel
+1  A: 

Use FIND_IN_SET.

SELECT id, pos, FROM $db WHERE dtime>='$now' AND FIND_IN_SET(jsub, '$arr[1]')
KennyTM