Mysql从数组中选择值

时间:2015-01-06 18:46:02

标签: php mysql arrays select

我创建了以下数组:

$selectedvillas[] = array (
    'villaname' => $row1['name']

print_r($selectedvillas);

打印:

Array ( [0] => Array ( [villaname] => Villa 1 ) [1] => Array ( [villaname] => Villa 2 ) [2] => Array ( [villaname] => Villa 3 ) [3] => Array ( [villaname] => Villa 4 ) [4] => Array ( [villaname] => Villa 5 ) [5] => Array ( [villaname] => Villa 6 ) [6] => Array ( [villaname] => Villa 7 ) [7] => Array ( [villaname] => Villa 8 ) [8] => Array ( [villaname] => Villa 9 ) [9] => Array ( [villaname] => Villa 10 ) [10] => Array ( [villaname] => Villa 11 ) [11] => Array ( [villaname] => Villa 12 ) )

我想从mysql中选择所有上面的别名,其中别墅是上面带别名的数组

$query_reviews= "SELECT * FROM reviews where villa IN $selectedvillas order by dateofreview DESC";

1 个答案:

答案 0 :(得分:0)

您必须将数组格式化为字符串。

首先,看起来您可以通过更简单地构建阵列来简化您的工作,如下所示:

$selectedvillas[] = $row1['name'];

然后,您可以使用implode()在逗号分隔的字符串中轻松格式化数组。

我经常使用这个:

$query_reviews= "SELECT * FROM reviews 
WHERE villa IN ('" .implode("', '", $selectedvillas) . "') order by dateofreview DESC";

注意:这些单引号很容易被遗漏,但它们很重要。