Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ok, I'm not seeing why this doesn't work, perhaps one of you gent's can help me out.

$new_upline = RetrieveUpline($root_referral_id, array());
echo ("The upline after return: <BR>");
var_dump ($new_upline);

function RetrieveUpline($root_ref_id, $upline){
    $referrer = mysql_query("SELECT id, username, referral_ID, total_points_earned,   isbanned FROM members WHERE id = ".$root_ref_id);
    $rows = mysql_num_rows($referrer);
    $upline_size = count($upline);

    if ($rows>0){
        while($feed = mysql_fetch_array($referrer, MYSQL_ASSOC)){
            $upline[$upline_size] = $feed;
            RetrieveUpline($upline[$upline_size]['referral_ID'], $upline);
        }
    }else{
        echo ("The upline before return: <BR>");
        var_dump($upline);
        return $upline;
    }
}

The var_dump inside the function works as expected. The return just returns nothing, even if I set it to raw text. I know it's probably something easy, but I'm burnt out on is right now.

share|improve this question
1  
You are not returning anything in your if branch – knittl Apr 20 '12 at 15:41
You should at least get NULL. – Marcus Adams Apr 20 '12 at 15:49

2 Answers

Try this version:

<?php

  function RetrieveUpline($root_ref_id, $upline = array()) {

    // Sanitize input
    $root_ref_id = (int) $root_ref_id;

    // Do query
    $query = "
      SELECT id, username, referral_ID, total_points_earned, isbanned
      FROM members
      WHERE id = $root_ref_id
    ";
    $result = mysql_query($query); // What if this query fails? Add error handling here...

    // Loop results
    while ($feed = mysql_fetch_assoc($result)) {
      $upline[] = $feed;
      $upline = RetrieveUpline($feed['referral_ID'], $upline);
    }

    // Free mysql result resource
    mysql_free_result($result);

    // Return new array
    return $upline;

  }

  $new_upline = RetrieveUpline($root_referral_id);
  var_dump($new_upline);

You need to either pass the $upline argument by reference, or return the result from either option - whether there are results or not. I would go for the returning every result option, as it means you don't need to initialise the result array before calling the function. The disadvantage to this approach is that it will be considerably more memory hungry, so you could use this version instead:

<?php

  function RetrieveUpline($root_ref_id, &$upline) {

    // Make sure $upline is an array (for first iteration)
    if (!is_array($upline)) $upline = array();

    // Sanitize input
    $root_ref_id = (int) $root_ref_id;

    // Do query
    $query = "
      SELECT id, username, referral_ID, total_points_earned, isbanned
      FROM members
      WHERE id = $root_ref_id
    ";
    $result = mysql_query($query); // What if this query fails? Add error handling here...

    // Loop results
    while ($feed = mysql_fetch_assoc($result)) {
      $upline[] = $feed;
      RetrieveUpline($feed['referral_ID'], $upline);
    }

    // Free mysql result resource
    mysql_free_result($result);

  }

  RetrieveUpline($root_referral_id, $new_upline);
  var_dump($new_upline);
share|improve this answer
Nice. I would just handle when $result is false, otherwise you will get a warning at the mysql_fetch_assoc() line if $result is false. – Marcus Adams Apr 20 '12 at 15:58
@MarcusAdams Indeed, I put a comment in the code to that effect, since I don't know how the OP wants to handle this (trigger_error(), return FALSE etc). I have put the (int) cast in to sanitise, so that anything that is not a valid numeric will be 0 and it won't cause a syntax error in the query. – DaveRandom Apr 20 '12 at 15:59
I didn't think about that. I suppose you could also use PDO and prepared statements if you had a wrapper function, and you passed in the handle to the prepared statement as one of the recursive parameters. Think about the performance gain if PHP didn't have to send the query each time and MySQL didn't have to parse it each time. – Marcus Adams Apr 20 '12 at 16:05

I know it was not the question but, maybe you should read something about getting "Hierarchical Data" and working with it. The way you do it now is very ... mh let me say, "slow".

share|improve this answer

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.