从两个表中选择,单循环

时间:2012-11-03 14:36:15

标签: php loops join

如何在单循环中创建此代码?从两张桌子中选出一张!

<?

$items = Array();
$kate=mysql_query("select category_id from movies_cat where movie_id='$ids'");

while($cat=mysql_fetch_array($kate))
{
  $kate2=mysql_query("select name_cat from categories where id='$cat[category_id]'");

  while($cat2=mysql_fetch_array($kate2))
  {
    $items[] = $cat2['name_cat'];
  }
}

echo implode(",", $items);

?>

1 个答案:

答案 0 :(得分:0)

查询应该是SELECT category_id, name_cat from movies_cat m join categories c where m.category_id = c.id and movie_id='$ids'";试试这个。