0

** 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";
   }
8
  • 1
    So what's the question? Commented Feb 15, 2013 at 19:50
  • mysql is only run on the first one. explain this line in more detail. Commented Feb 15, 2013 at 19:52
  • Do a var_dump on $getactors, is it the array you expect? If it is iterating more than once with the foreach, the issue might be the value in the where clause has some extra whitespace and the SQL returns no results. Could just echo $srch3 in the loop to see what your building out. Commented Feb 15, 2013 at 19:55
  • if $actors has more than one actor in it, mysql is only being run on the first actor returned from the explode. None of the others ever return an $idolid. Commented Feb 15, 2013 at 19:56
  • And if a value in $getactors includes an apostrophe, for example, the unescaped query will be broken. Commented Feb 15, 2013 at 19:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.