将PHP变量值传递给SQL IN子句

时间:2014-05-05 18:03:23

标签: php mysql sql

我有这个SQL查询。

  select something from sometable WHERE `word` IN ('stand','on','in') GROUP BY (`p_id`)  LIMIT 1000

我有一个php变量($ words),它持有一个句子。如何将该变量值传递给这个IN子句?

ex:$ words =“站在这里”;

1 个答案:

答案 0 :(得分:1)

$array = explode(" ", $words); // GEt each word in array
$in_stmt = "'".implode("','", $array)."'"; // create a string like 'stand','on','in'

<强>更好

$in_stmt = "'".str_replace(" ", "','", $words)."'";

MySQL声明:

$stmt = "select something from sometable WHERE `word` IN (".$in_stmt.") GROUP BY (`p_id`)  LIMIT 1000"