while循环中的foreach循环

时间:2013-01-15 02:16:53

标签: php loops foreach while-loop

也许我正在混合苹果和橘子 - $ clist是一个带有csv的字符串。我无法得到$ row2 ['$ cat']来回应任何事情。提前致谢

$cats = explode(",", $clist);
foreach($cats as $cat) {
  $cat = trim($cat);
  $q = "SELECT * FROM contacts WHERE id = '$cat' ";
  $result = mysql_query($q, $link);

  $num2=mysql_num_rows($result2);

  $x = 0;
  while($x < $num2) {
    $row2 = mysql_fetch_array($result2);
    echo $row2['$cat'];
    $x++;
  }
}

2 个答案:

答案 0 :(得分:2)

来自Michael Berkowski的评论:删除引号。 $row2[$cat]

答案 1 :(得分:0)

为什么不进行单个查询,而不是对数组中的每个值进行查询?

$cats = array('ginger','tabby','somthing');
$sql = "SELECT * FROM contacts WHERE id IN(\"".implode('", "',$cats)."\")";

//SELECT * FROM contacts WHERE id IN("ginger", "tabby", "somthing")