0

Still just learning PHP so sorry if this is stupid!! I have a function that loops through a mysql adjacency table. I would like to return this as an array. It seems fine, in that if when I return in the function I do print_r($path); where $path is the array inside the function, the array is printed as I want it.

The problem is I cannot see why I cannot access it via

$patharray = functioncalled($vars);

I have tried using return $path; and also just print_r in the array, but it keeps returning NULL.

function getParPath($mysqli, $showpar, $path) {
    // Get Parent Path
    $query ="select loc_id, loc_name, loc_parent from locations where loc_id = ? LIMIT 0,1";
    if ($stmt = mysqli_prepare($mysqli, $query)) {

        /* pass parameters to query */
        mysqli_stmt_bind_param($stmt, "s", $showpar);

        /* run the query on the database */
        mysqli_stmt_execute($stmt);

        /* store result */
        mysqli_stmt_store_result($stmt);

        /* assign variable for each column to store results in */
        mysqli_stmt_bind_result($stmt, $loc_id, $loc_name, $loc_parent);

        /* fetch values */
        while (mysqli_stmt_fetch($stmt)) {

            $path[] = $loc_name;

            if ($loc_parent !='0'){
                getParPath($mysqli, $loc_parent, $path);
            }
            if ($loc_parent =='0'){
                $path = array_reverse($path);
                return $path; // uncommented per answers below.
                //print_r($path); //commented out per answers below.
            }

        }
    }
}

$patharray = getParPath($mysqli, $showcld, $path);
//print_r(getParPath($mysqli, $showcld, $path)); // removed per answers below
var_dump($patharray); //removed echo per answers below
exit;

Which returns:

Array
(
    [0] => BuildingA
    [1] => FloorA
    [2] => RoomA
)
Array
(
    [0] => BuildingA
    [1] => FloorA
    [2] => RoomA
)
NULL

So as you can see from the last line there, the array $patharray I set to the function call is NULL. And that if I try to print_r inside the function, an array is printed, and if I try to print_r the function directly, it returns an array. But not into an object I can use.

1 Answer 1

3

You have to return $path; not just echo or print_r.

$path = array_reverse($path);
return $path;  //Uncomment this line
//print_r($path); //Comment out this line

Also you need to change this, you weren't setting $path equal to the results:

if ($loc_parent !='0'){
    $path = getParPath($mysqli, $loc_parent, $path);
}

Also get rid of the if test because it will keep running until it gets to the bottom then you need to return.

if ($loc_parent !='0'){
    $path = getParPath($mysqli, $loc_parent, $path);
}
$path = array_reverse($path);
return $path;
9
  • 1
    Lol you even have it in there it is just commented out. Commented May 20, 2013 at 16:23
  • Thanks-but... I did have this in originally and it didn't work, this is why I commented it out and put print_r in instead, so I could see what what actually the output of the function. Is there something else wrong? Commented May 20, 2013 at 16:30
  • If I comment the print_r and uncomment the return line, it just results in NULL Commented May 20, 2013 at 16:31
  • Why do you have echo vardump() vardump() echos itself you don't need echo. Also comment out the print_r(getParPath($mysqli, $showcld, $path)); this is redundant and makes debugging harder. Commented May 20, 2013 at 16:34
  • I didn't know that about vardump thanks, I removed the print_r now, I have updated the code in the question to as it is per your comments. It just returns NULL Commented May 20, 2013 at 16:37

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.