Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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;
share|improve this answer
1  
Lol you even have it in there it is just commented out. –  Pitchinnate May 20 '13 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? –  Joeme May 20 '13 at 16:30
    
If I comment the print_r and uncomment the return line, it just results in NULL –  Joeme May 20 '13 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. –  Pitchinnate May 20 '13 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 –  Joeme May 20 '13 at 16:37
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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