-1

I have created a function(recursive). It will call again if it has parent id otherwise it will return last Id. The Problem is I'm unable to return the last Id which passed in the Function argument. It is showing complete if I use var_dump() or echo but unable to return in return().

Here is Sample Code :

function abc($dbcon,$id){
    $get_quy = "select * from member_mst where member_status=0 and pid=".$id;
    $get_rs = $dbcon->query($get_quy);

    $get_numrows = mysqli_num_rows($get_rs);
    $get_rel = mysqli_fetch_assoc($get_rs);

    if($get_numrows>0){
        abc($dbcon,$get_rel['member_id']);
    }else{
        //var_dump($id);//This will return => "String '3'" 
        return $id;
    }
}
var_dump(abc($dbcon,1));//Returning Null 
Phil
  • 157,677
  • 23
  • 242
  • 245
abhi chavda
  • 153
  • 1
  • 11

1 Answers1

0

Here is the working code. The Problem was I wasn't using return() while calling the function again.

function abc($dbcon,$id){
    $get_quy = "select * from member_mst where member_status=0 and pid=".$id;
    $get_rs = $dbcon->query($get_quy);

    $get_numrows = mysqli_num_rows($get_rs);
    $get_rel = mysqli_fetch_assoc($get_rs);

    if($get_numrows>0){
        return abc($dbcon,$get_rel['member_id']); // Updated Code Here
    }else{
        return $id;
    }
}
var_dump(abc($dbcon,1));//Returning "3"
abhi chavda
  • 153
  • 1
  • 11