Matthew Kyle
Matthew Kyle

Reputation: 1

Get Multiple Rows in PHP / MYSQL

I am trying to pull the image link for 10 different rows based on ids in an array. It seems to break when the query is in the loop..Any ideas how i can accomplish this?

$array = array(54, 319, 342, 298, 281, 190,178,158,138,7);


$shuffleKeys = array_keys($array);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
    $newArray[$key] = $array[$key];
}


for($i=0;$i<=count($newArray);$i++){

    $query = "SELECT logoName,logoImageLink, logoImageLink2, countryImg, logoArtist, afterText, country FROM logos WHERE id = $array($i)     ";
    $result = mysql_query($query);


    /* fetch rows in reverse order */
    for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) {
    if (!mysql_data_seek($result, $i)) {
        echo "Cannot seek to row $i: " . mysql_error() . "\n";
        continue;
    }

    if (!($row = mysql_fetch_assoc($result))) {
        continue;
    }

    $imageLink = $row['logoImageLink'];

    echo "<li class=\".$array($i).\" ><img src=\".$imageLink.\" /></li>";
}

Upvotes: 0

Views: 122

Answers (1)

Brian Kerr
Brian Kerr

Reputation: 401

You can use the MySQL IN clause and do this in a single select.

$ids = join(',',$newArray);  
$query = "SELECT logoName,logoImageLink, logoImageLink2, countryImg, logoArtist, afterText, country FROM logos WHERE id IN ($ids)";
$result = $mysqli->query($query);

while ($row = $result->fetch_assoc()) {
     $imageLink = $row['logoImageLink'];

     echo "<li><img src=\"$imageLink\"/></li>";
}

Upvotes: 2

Related Questions