查询DB以获取给定数组中的列值

时间:2013-01-06 21:38:45

标签: php mysql sql

我正在尝试查询数据库中的表,其中列home_team和away_team包含数组中的值。然而,没有这样的运气。我的代码如下。

$new_array = array();

for($i=0;$i<count($ncaa);$i++)
{
//  Since some of the values in this array contain apostrophes, escape each value 
$new_array[$i] = mysql_real_escape_string($ncaa[$i]);   
}

$list = join(",", $new_array);
$query = "SELECT * 
     FROM ncaa_games
     WHERE game_date_int >= '".$limit."' 
     AND (home_team = '".mysql_real_escape_string($team)."' OR away_team = '".mysql_real_escape_string($team)."') 
     AND home_team IN (".$list.")
     AND away_team IN (".$list.")
     ORDER BY game_date_int ASC 
     LIMIT 1";

$next = mysql_query($query)or die(mysql_error()); 

我一直收到一条错误消息“你的SQL语法有错误;请查看与你的MySQL服务器版本相对应的手册,以便在''UTSA Roadrunners'附近使用正确的语法”)和away_team IN(Albany Great)丹麦人,宾厄姆顿熊'在第5行“

我觉得这可能是相当简单的事情。但是,它仍然无法逃避我。有什么想法吗?

谢谢,

兰斯

1 个答案:

答案 0 :(得分:2)

$list = join(",", $new_array);

不会将球队用引号括起来。即你有:

away_team IN (Albany Great Danes,Binghamton Bears)

这应该是:

away_team IN ('Albany Great Danes','Binghamton Bears')

尝试使用:     $ list = join(“','”,$ new_array); 并拥有:

 AND home_team IN ('".$list."')
 AND away_team IN ('".$list."')

(确保$list不为空。)