[英]result of one mysql query in another query using php
i have a mysql query joining two tables, putting results in associative array using mysql_fetch_array and displaying 'name of cities' using while loop. 我有一个连接两个表的mysql查询,使用mysql_fetch_array将结果放入关联数组中,并使用while循环显示“城市名称”。 In the same query, I need to do one more query using each corresponding 'city name' of associative array and show corresponding tags of each city.
在同一查询中,我需要使用关联数组的每个对应“城市名称”再进行一次查询,并显示每个城市的对应标签。
$result = mysql_query("select city.city_ID, city.city_name, city.city_page, city_images.cimage_path from city, city_images where
city.city_ID=city_images.city_ID");
while ($row = mysql_fetch_array($result)) {
echo '<div class="Box">';
echo '<a href="' . $row['city_page'] . '">';
echo '<img src=" ' . $row['cimage_path'] . ' " />';
echo '</a>';
echo '<div class="imagetext">';
echo '<a href="' . $row['city_page'] . '">' . $row['city_name'] . '</a>';
echo '</div>';
echo '<div class="Inbox">';
$r = mysql_query("select Tag_Name from tag, city_tag where city_tag.city_ID =$row ['city_ID'] and city_tag.Tag_ID=tag.Tag_ID ");
while ($raw = mysql_fetch_array($r)) {
echo '<div class="boxtags">';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
I need tags corresponding to current city..pls help 我需要与当前城市相对应的标签..pls帮助
You only need one query... 您只需要一个查询...
SELECT c.city_ID
, c.city_name
, c.city_page
, ci.cimage_path
, t.Tag_Name
FROM city c
JOIN city_images ci
ON ci.city_ID = c.city_ID
JOIN city_tag ct
ON ct.city_ID = c.city_ID
JOIN tag t
ON tag.Tag_ID = ct.Tag_ID;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.