将 PHP 变量值传递给 SQL IN 子句
问题描述:
我有这个 sql 查询.
I have this sql query.
select something from sometable WHERE `word` IN ('stand','on','in') GROUP BY (`p_id`) LIMIT 1000
我有一个 php 变量 ($words) 来保存一个句子.如何将该变量值传递给这个 IN 子句?
And I have a php variable ($words) which holds a sentence. How to pass that variable value into this IN clause?
例如:$words = "站在这这里";
ex: $words = "stand on in this here";
答
$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"