0

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.

2
  • 1
    You are not returning anything in your if branch Commented Apr 20, 2012 at 15:41
  • You should at least get NULL. Commented Apr 20, 2012 at 15:49

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
@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.
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.
0

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".

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.