2013-10-10 3 views
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"; 
} 

,하지만 그렇게 모든 제품이까지 normaly 제공 최초 제품 정보, 다른 제품에 대한 다음 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 테이블 : http://i.imgur.com/ROjBQT0.png

attribute_description 테이블 : 당신은 두 번 모두를 얻고있다 http://i.imgur.com/6M5cNXi.png

+1

나는 당신이하려고하는 것이 확실하지 않지만, 당신의 쿼리에서'JOIN'은 루프에서 db를 쿼리 할 필요가 없으므로 많은 작업을 절약 할 것이라고 생각합니다. – TheWolf

+0

테이블 구조와 원하는 출력 예를 게시하십시오. – TheWolf

+0

코드가 누락되었지만 제품간에'$ attr_t '를 재설정하지 않은 것 같습니다. 당신은 그것을 영원히 덧붙이므로 각 제품의 결과는 그 이후의 모든 제품에도 적용됩니다. – Izkata

답변

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 
+0

그러면 제품 ID가있는 모든 제품 속성 및 설명이 제공됩니다. product_attrbute. * 선택 product_attribute product_attribute.product_id = –

0

때문에 숫자와 문자열 인덱스를 모두 데이터베이스에서 mysql_fetch_array 값을 반환. mysql_fetch_assoc을 사용하면 문제가 해결됩니다.

관련 문제