遇到循环动态mysql数据数组的问题

时间:2012-09-09 12:19:59

标签: php mysql arrays multidimensional-array

我有一个数组,一个例子是..

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 个答案:

答案 0 :(得分:1)

它不会进入的数组是空的,因为我假设因为我没有从相应的查询中得到任何错误,查询没问题,但事实并非如此 - 其中一个搜索参数是空白的。

如果您遇到这样的问题,总是输出相应的查询并手动运行以查看您是否有空的结果集。