我有这个代码:
$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>
等等...有什么想法吗?
编辑
product_attribute表:https://i.stack.imgur.com/ItAEi.png
attribute_description表:https://i.stack.imgur.com/Kjvzu.png
我认为最好使用 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
您将获得两次所有内容,因为mysql_fetch_array
从数据库中返回带有数字和字符串索引的值。使用mysql_fetch_assoc
应该可以解决问题。