我想要做的是,例如: 我有一个字符串,我用作爆炸的数组:
$string = "Hello Cruel World";
$string = $explode(' ', $string);
我如何使用值$string[0]
,$string[1]
作为mysqli_query()
select
?例如:content LIKE $string[0] OR $string[1]
....
但是$string
值可以是任何值,所以我也需要具有数组的大小。
有办法做到这一点?来自select LIKE
的{{1}}?
答案 0 :(得分:0)
$string = 'Hello Cruel World';
$parts = explode(' ', $string);
$escaped = array();
foreach($parts as $part) {
$escaped[] = mysql_real_escape_string($part);
}
$sql = "SELECT ... FROM ... WHERE yourfield IN ('" . implode("','", $escaped) . "');";
应生成
SELECT ... FROM ... WHERE yourfield IN ('Hello','Cruel','World')
用于通配LIKE比较
$escaped[] = "yourfield LIKE '%" . mysql_real_escape_string($part) . "%'";
$sql = "... WHERE " . implode(' OR ', $escaped);
给
... WHERE yourfield LIKE '%Hello%' OR yourfield LIKE '%Cruel%' OR yourfield LIKE '%World%'