0

I have created a function as below:

<?php
$cat_id=2;
$ids=array();
$ids[]=2;
var_dump(fetchParents($cat_id,$ids));
function fetchParents($cat_id,$ids){
    $db=mysql_connect("localhost","root","") or die("could not connect to database");
    mysql_select_db("symfony",$db) or die("could not connect to symfony database");
    $query="SELECT parent_id FROM categories WHERE id='$cat_id'";
    $result=mysql_query($query,$db);
    if (mysql_num_rows($result)==1) {
        $cat=mysql_fetch_array($result);
        if (isset($cat['parent_id'])) {
             $ids[]=$cat['parent_id'];
             fetchParents($cat['parent_id'],$ids);
        }
        else {
            var_dump($ids);
            return $ids;
        }

    }
    else {
        return $ids;
    }
}
?>

The function should return an array but I am getting null and I m sure $ids is an array. I have also checked using var_dump($ids) inside the function which returns array. Can somebody help me ?

2
  • you might want to use a different name for the input $ids and the output $ids Commented Mar 13, 2013 at 7:40
  • Also, this can be rewritten to a simple loop - no need to involve a costly recursion. Commented Mar 13, 2013 at 7:41

2 Answers 2

3

There is one control flow that does not perform a return statement before exiting the function. You need to change

fetchParents($cat['parent_id'],$ids);

to

return fetchParents($cat['parent_id'],$ids);

Otherwise after that line the function exits without a return.

Added:

To convert to a loop:

function fetchParents($childId)
{
    $ids = array();

    $db=mysql_connect("localhost","root","") or die("could not connect to database");
    mysql_select_db("symfony",$db) or die("could not connect to symfony database");

    while (true)
    {
        $res = mysql_query("SELECT parent_id FROM categories WHERE id=$childId");
        $row = mysql_fetch_assoc($res);
        if (is_null($row['parent_id']) )
            return $ids;
        $ids[] = $row['parent_id'];
        $childId = $row['parent_id'];
    }
}

Btw - I see that in the original example you also add the ID of the starting element to the array too. If so, then it can be made even more elegant:

function fetchParents($childId)
{
    $ids = array();

    $db=mysql_connect("localhost","root","") or die("could not connect to database");
    mysql_select_db("symfony",$db) or die("could not connect to symfony database");

    while (!is_null($childId))
    {
        $ids[] = $childId;
        $res = mysql_query("SELECT parent_id FROM categories WHERE id=$childId");
        $row = mysql_fetch_assoc($res);
        $childId = $row['parent_id'];
    }
    return $ids;
}

Note that both of these don't handle the case when the SELECT query returns no rows. I assume that that is something that won't happen, since you're selecting by IDs. If however that is a possibility, then you also need to check for $row === false, or you'll get a lot of warning/error messages (not sure what PHP will do in this case).

Sign up to request clarification or add additional context in comments.

3 Comments

@PradipChitrakar - rewrite it as a loop. It will be a LOT simpler, and thus harder to mess up.
how can i do this with loop ? any hint. I need to fetch all the parents of a child
@PradipChitrakar - I edited the post, check it out. The idea is simply that at the end of each iteration you set the $childId to $row['parent_id'] and start all over again.
0

If the two first IF are true then the function don't return nothing.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.