0

我有这个代码:

    $atrr_row = mysql_query("SELECT * FROM " . DB_PREFIX . "product_attribute WHERE product_id='".$r['product_id']."' and language_id=1");
while($atr=mysql_fetch_array($atrr_row)){
    $attr_n_g = mysql_fetch_array(mysql_query("SELECT name FROM " . DB_PREFIX . "attribute_description  WHERE attribute_id  = '" . $atr['attribute_id'] . "' and language_id=1 LIMIT 1"));
    $attr_t.="\t".'<spec name="'.$attr_n_g['name'].'"><![CDATA['.$atr['text'].']]></spec>'."\n";
}

它应该给出来自 2 个表的结果,但它通常首先给出产品信息,然后给出另一个产品的 x2,依此类推,直到所有产品都循环并且结果乘以 x375。

Looped data:
First product:
<specs>
<spec name="Age rating">
<![CDATA[18+]]>
</spec>
<spec name="Release date">
<![CDATA[2012]]>
</spec>
<spec name="Online mode">
<![CDATA[No]]>
</spec>
<spec name="Metacritic score">
<![CDATA[75 - 89]]>
</spec>
</specs>

Second product:
<specs>
<spec name="Age rating">
<![CDATA[18+]]>
</spec>
<spec name="Release date">
<![CDATA[2012]]>
</spec>
<spec name="Online mode">
<![CDATA[No]]>
</spec>
<spec name="Metacritic score">
<![CDATA[75 - 89]]>
</spec>
<spec name="Age rating">
<![CDATA[16+]]>
</spec>
<spec name="Release date">
<![CDATA[2011]]>
</spec>
<spec name="Online mode">
<![CDATA[Yes]]>
</spec>
<spec name="Metacritic score">
<![CDATA[90 - 100]]>
</spec>
</specs>

等等......有什么想法吗?

编辑

产品属性表:http: //i.imgur.com/ROjBQT0.png

属性描述表:http: //i.imgur.com/6M5cNXi.png

4

2 回答 2

0

您将获得所有内容两次,因为mysql_fetch_array从数据库中返回带有数字和字符串索引的值。使用mysql_fetch_assoc应该可以解决问题。

于 2013-10-10T19:30:14.993 回答
0

我认为您最好使用 JOIN 查询而不是像这样循环。

"SELECT product_attribute.*, attribute_description.name FROM product_attribute LEFT JOIN product_description on product_description.attribute_id = product_attribute.id 
WHERE product_attribute.product_id='".$r['product_id']."'and language_id=1
于 2013-10-10T19:30:22.967 回答