开发者

mysql IN parameter

开发者 https://www.devze.com 2022-12-31 00:55 出处:网络
When the user check more than one (checkbox) option which are then combine into a string of \"apple,orange,pear\"

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开发者_JAVA技巧 function to process correctly?


use:

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

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


Use FIND_IN_SET.

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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号