将 PHP 变量值传递给 SQL IN 子句 [英] pass PHP variable value into SQL IN clause
本文介绍了将 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"
这篇关于将 PHP 变量值传递给 SQL IN 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文