mysql - 如何在mysqli_query中选择一个像数组一样的值?

时间:2014-04-07 15:02:38

标签: php arrays

我想要做的是,例如: 我有一个字符串,我用作爆炸的数组:

$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}}?

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%'