你好,我试图计算有多少行位于列中,其中名称包括任何名称在预定义的$ uk_array。MySQL like子句不能对数组php
mysql打印出我没有任何值的列“有()”与数组中的名称相匹配,但有一个“Dublin,Ireland”。
下面是代码:
<?php
include'connect.php';
$uk_array = array('Liverpool','London','London UK','UK','London',
'Dublin, Ireland','Manchester','Norwich','United Kingdom','Norwich','Duplin','England','ENGLAND',
'united kingdom');
$string = '';
foreach($uk_array as $term=>$i) {
$string = $string."location LIKE '%".$i."%' AND";
}
$string = substr($string, 0, -5);
$query="SELECT COUNT(location) as location FROM tweets WHERE $string";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "There are ". $row['location']."";
}
?>
编写代码之前,更注重你的逻辑。 – Matt 2012-07-30 13:17:55
如果你没有部分地点匹配,可以使用WHERE IN IN('Dublin','Liverpool','London',...)'而不是LIKE,它的速度更快,更干净。 – ddinchev 2012-07-30 13:18:50
缺少空间。这就是我要说的...... – DaveRandom 2012-07-30 13:20:03