简体   繁体   中英

Mysql SELECT WHERE id = array()

Im trying to insert an array into a SELECT WHERE id = array() and im trying to show the results in a table,i can get one row of data, the first teams name,wins,and losses, but the remaining 4 teams do not appear.Im trying to show all teams where there id's are equal to the array.

$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>';
    }`

You need to loop through your results, right now you if you get 1 or 10 results, you only print the first item.

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

Lets consider that your $teamx array has values as below:

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

Then your query should be as below:

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

There is no need for array_map function.

The error may be in your $tournament['teams']; . Check values in it and if the values are like my above $teamx array, then you have to loop through your $tournament['teams']; to generate an array.

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>';
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM