mysql join和fetch

时间:2015-03-02 17:04:28

标签: mysql xml join

我已经尝试搜索主题并尝试我所知道的解决这个问题,但不幸的是我仍然不能。

我现在想要在谷歌地图infowindows中包含一些价值。 我从谷歌开发者那里找到了一些来源,但现在我需要结合2个表的信息并为谷歌地图案例生成一个xml。

表1

id    |    tid    |   hotelprice   |  attachment
1          234          100             http://xxx.xxx.xxx
2          345          106             null
3          663          905             null

表2

name    |    url    |  description   | lat  | lng    | type  | area
ABC          111       lorumop111      2.12   -109.1   poi     us
EFG          234       lorumop234      2.13   -109.2   hotel   us
HIJ          345       lorumop345      2.14   -109.2   hotel   us

我需要将值输出为XML,首先我查询

$queryprice = mysql_query("SELECT t1.tid, t2.url, t1.hotelprice 
                           FROM t1 INNER JOIN t2 ON t2.url = t1.tid;");

之后我也查询

$query = mysql_query("SELECT * FROM t2 WHERE area = '".$area."'");

之后我需要使用源输出为XML,

while (($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($queryprice))){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
  $newnode->setAttribute("description", $row['description']);
  $newnode->setAttribute("url", $row['url']);
  $newnode->setAttribute("area", $row['area']);
  $newnode->setAttribute("hotel_bookingprice", $row2['hotelprice']);
}

echo $dom->saveXML();

但是当输出为XML时,酒店价格不正确。

<markers>
<marker name="ABC" lat="2.12" lng="-109.1" type="poi" description="lorumop111" url="111" area="us" hotelprice="NOT correct amount even this amount can't found in this table, this should be 0 or null because t1 don't have tid=111"/>
<marker name="EFG" lat="2.13" lng="-109.2" type="hotel" description="lorumop234" url="234" area="us" hotelprice="NOT correct amount"/>
</markers>

我需要的是:

<marker name="ABC" lat="2.12" lng="-109.1" type="poi" description="lorumop111" url="111" area="us" hotelprice="0"/>
<marker name="EFG" lat="2.13" lng="-109.2" type="hotel" description="lorumop234" url="234" area="us" hotelprice="100"/>
<marker name="HIJ" lat="2.14" lng="-109.3" type="hotel" description="lorumop345" url="345" area="us" hotelprice="106"/>

我希望你的家伙能理解我说的话,谢谢你。

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

$query = mysql_query("SELECT t2.*, t1.hotelprice FROM t2 
                          LEFT JOIN t1 ON t2.url = t1.tid
                      WHERE area = '".$area."'");

while ( $row = mysql_fetch_array($query) ){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name",$row['name']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("type", $row['type']);
  $newnode->setAttribute("description", $row['description']);
  $newnode->setAttribute("url", $row['url']);
  $newnode->setAttribute("area", $row['area']);
  $newnode->setAttribute("hotel_bookingprice", $row['hotelprice']);
}