0

I have an array, an example being..

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
        )

)

Now, the cats key is a MySQL data set, which I have no issues looping through with mysql_fetch_array; however once inside that loop I try and run another loop on a certain key of the listings array, such as home-and-garden, all the keys under the listings array are dynamic, so I have to pass in a variable with the key name, however it won't enter the loop.

Below is an example of my code..

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                

        }

    }

}

Edit: Dump of an example $sql['listings'][$main_category_slug] is below:

resource(55) of type (mysql result) 

Dump of $sql['listings'] is below:

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)
}

They all appear to be valid resources and I have checked that the key name is correct.

9
  • 1
    You're aware that mysql_fetch_array() without a second parameter will return both numeric and associative keys, right? so list() won't get the values you expect. Instead call as mysql_fetch_array($sql['cats'], MYSQL_ASSOC) or use mysql_fetch_assoc() for both those loops. Sep 9, 2012 at 12:25
  • And What is in $sql['listings'][$main_category_slug]? Is it actually a mysql result resource? Sep 9, 2012 at 12:26
  • check that you're not calling mysql_free_result() somewhere for your resources... also, could you possibly include a dump of the mysql_fetch_array($sql['listings'][$main_category_slug]) part? Sep 9, 2012 at 12:31
  • @MichaelBerkowski Yes I'm aware that it returns both and I don't really need both, it's just a bad habit of mine to keep using it. However, I have never has issues using it like that in the past.
    – Brett
    Sep 9, 2012 at 12:38
  • @MichaelBerkowski As you can see in the example array output above $sql['listings'][$main_category_slug] is a MySQL resource. $main_category_slug is equal to one of those names.
    – Brett
    Sep 9, 2012 at 12:39

1 Answer 1

1

The array it wouldn't go into was empty because I assumed since I wasn't getting any errors from the respective queries that the query was ok, but it wasn't - one of the search parameters was blank.

If you are getting problems like this, always output the respective query and run it manually to see if you have an empty result set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.