Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php while()循环的结果太多了_Php_Mysql_While Loop - Fatal编程技术网

Php while()循环的结果太多了

Php while()循环的结果太多了,php,mysql,while-loop,Php,Mysql,While Loop,我有以下代码: $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 FR

我有以下代码:

    $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";
}
它应该给出两个表的结果,但它给出的通常是第一个产品信息,然后是另一个产品的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>
等等。。。有什么想法吗

编辑

产品属性表:

属性描述表:

由于mysql\u fetch\u数组使用数字索引和字符串索引从数据库返回值,因此您将两次获取所有内容。使用mysql\u fetch\u assoc应该可以解决这个问题。

我认为最好使用连接查询,而不是像这样循环

"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

我不太确定您想做什么,但我想查询中的联接将为您节省大量工作,因为您不必在循环中查询数据库。请发布您的表结构和所需输出的示例。缺少代码,但看起来您只是没有在产品之间重置$attr\t。您将永远附加到它,因此每个产品的结果也将出现在它之后的所有产品上。这将为您提供具有产品id的所有产品属性及其说明。您可以尝试:从product_attribute中选择product_attribute.*其中product_attribute.product_id=