Mysql SELECT WHERE id = array()

时间:2014-12-07 06:32:05

标签: php mysql arrays

我试图将一个数组插入SELECT WHERE id = array()并且我试图在表中显示结果,我可以得到一行数据,第一个队名,胜利和损失,但剩下的4个团队没有出现。我试图向所有团队展示id等于阵列。

$tournament = mysql_query("SELECT * FROM `tournaments` WHERE `id` = ".((int)$slug[1])." LIMIT 1");
    if (mysql_num_rows($tournament) > 0) {
        $tournament = mysql_fetch_assoc($tournament);
    }
    //array is equal to 273,287,234,423,124
    $teamx = array();
    $teamx[] = $tournament['teams'];

    $tteams = mysql_query("SELECT * FROM `teams` WHERE `id` IN (" . implode(',', array_map('intval', $teamx)) . ") LIMIT 128");
    if (mysql_num_rows($tteams) > 0) {
        $tteams = mysql_fetch_assoc($tteams);
        $template['TOURNAMENTTEAMS'] .= '<tr><td style="text-align: center;"><strong>' .$tteams['name']. '</td><td style="text-align: center;">' .$tteams['wins']. '</td><td style="text-align: center;">' .$tteams['losses']. '</td></tr>';
    }else{
        $template['TOURNAMENTTEAMS'] .= '<tr><td style="text-align: center;"><strong>No eligible teams.</td><td style="text-align: center;"></td><td></td></tr>';
    }`

3 个答案:

答案 0 :(得分:0)

你需要循环搜索结果,现在你得到1或10个结果,你只打印第一个项目。

if (mysql_num_rows($tteams) > 0) {
    $results = mysql_fetch_assoc($tteams);
    foreach($results as $row) {
        $template['TROURNAMENTTEAMS'] .= ... ;
    }
} else {
    $template['TROURNAMENTTEAMS'] .= ... ;
}

答案 1 :(得分:0)

让我们考虑一下您的$teamx数组的值如下:

$teamx = array(273,287,234,423,124);

然后您的查询应如下所示:

    $tteams = mysql_query("SELECT * FROM `teams` WHERE `id` IN (" . implode(',', $teamx)) . ") LIMIT 128");

不需要array_map功能。

错误可能出在您的$tournament['teams'];中。检查其中的值以及值是否与上面的$teamx数组类似,然后您必须遍历$tournament['teams'];以生成数组。

答案 2 :(得分:0)

if (mysql_num_rows($result) > 0) {
        while ($tteamsfetch = mysql_fetch_assoc($result)){
            $template['TOURNAMENTTEAMS'] .= '<tr><td style="text-align: center;"><strong>' .$tteamsfetch['name']. '</td><td style="text-align: center;">' .$tteamsfetch['wins']. '</td><td style="text-align: center;">' .$tteamsfetch['losses']. '</td></tr>';
        }
    }