简体   繁体   中英

php loop mysql only executing once

** ANSWERED .. Was whitespace after commas. Thanks everyone!

When I execute the following code, all $castmember are displayed however mysql is only run on the first one.

   $getactors=explode(",", $actors);
   foreach($getactors as $castmember){

     $idolid="";
     $srch3= "SELECT `id`,`dir` FROM `idols` WHERE `name`='$castmember'";
     $result5 = mysql_query($srch3);
     while ($row5 = mysql_fetch_assoc($result5)) {
     $idolid = $row5["id"];
     $idoldir= $row5["dir"];
     }

   if($idolid){ echo"<li style=\"font-size:1.2em;\" ><a href=\"/gallery/$idolid/$idoldir.html\" title=\"$castmember\" itemprop=\"actors\"> $castmember</a>";
     } else {
   echo"<li style=\"font-size:1.2em;\"><div style=\"display:inline\" itemprop=\"actors\"> $castmember</div>";
     }
   echo"<span></span></li>\n";
   }

You don't need to explode and then run query in loop. You can use IN clause.

 $srch3= "SELECT `id`,`dir` FROM `idols` WHERE `name` IN ('".$actors."')";

This will work only if your $actors array has valid values though.

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