2012-09-09 38 views
0

我有一個數組,一個例子是..有通過動態MySQL數據數組循環問題

Array 
(
    [cats] => Resource id #54 
    [listings] => Array 
     (
      [home-and-garden] => Resource id #55 
      [professional-services] => Resource id #56 
      [community] => Resource id #57 
      [education-and-instruction] => Resource id #58 
      [automotive] => Resource id #59 
      [legal-and-financial] => Resource id #60 
     ) 

) 

現在,cats關鍵是MySQL的數據集,我覺得沒有問題mysql_fetch_array循環通過;然而,一旦進入該循環,我嘗試在列表數組的某個鍵上運行另一個循環,例如home-and-garden,列表數組下的所有鍵都是動態的,所以我必須傳遞一個帶有鍵名的變量,但它贏了進入循環。

下面是我的代碼示例..

protected function makePopularCategoryHTML($sql) { 

    while (list($main_category,$slug,$image)=mysql_fetch_array($sql['cats'])) { 

     // Make lowercase category slug 
     $main_category_slug = URLSafe($main_category); 

     while (list($category,$name,$tag1,$newurl)=mysql_fetch_array($sql['listings'][$main_category_slug])) { 

      // It won't enter this loop     

     } 

    } 

} 

編輯:轉儲一個例子$sql['listings'][$main_category_slug]的低於:

resource(55) of type (mysql result) 

$sql['listings']轉儲低於:

array(6) { 
    ["professional-services"]=> 
    resource(55) of type (mysql result) 
    ["home-and-garden"]=> 
    resource(56) of type (mysql result) 
    ["community"]=> 
    resource(57) of type (mysql result) 
    ["food-and-dining"]=> 
    resource(58) of type (mysql result) 
    ["real-estate"]=> 
    resource(59) of type (mysql result) 
    ["business-to-business"]=> 
    resource(60) of type (mysql result) 
} 

他們都似乎是有效的資源資源和我已檢查密鑰名稱是否正確。

+1

你知道沒有第二個參數的'mysql_fetch_array()'會返回_both_數字和關聯鍵,對不對?所以'list()'不會得到你期望的值。相反,調用mysql_fetch_array($ sql ['cats'],MYSQL_ASSOC)'或使用'mysql_fetch_assoc()'作爲這兩個循環。 –

+0

什麼是在$ sql ['列表'] [$ main_category_slug]'?它實際上是一個MySQL結果資源? –

+0

檢查你是否沒有爲你的資源調用mysql_free_result()...另外,你可能包含mysql_fetch_array($ sql ['listing'] [$ main_category_slug])部分的轉儲嗎? –

回答

1

它不會進入的數組是空的,因爲我假設,因爲我沒有從各個查詢中得到任何錯誤,查詢正常,但它不是 - 搜索參數之一爲空。

如果你遇到這樣的問題,總是輸出相應的查詢並手動運行它,看看你是否有一個空的結果集。