0

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?

3
  • a value passed from Flash to php Commented May 22, 2010 at 10:12
  • WHAT value? Got an example? Are you thinking we all has telepathy here? Commented May 22, 2010 at 10:15
  • Nevermind, solve my problem using replace string. Commented May 22, 2010 at 10:21

3 Answers 3

2

use:

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

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

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

1 Comment

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]);
1

Use FIND_IN_SET.

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

Comments

0

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?

1 Comment

I suspected something of the kind. Lack of debugging, as usual. Home my simple checklist'll help

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.