-3

Could you anybody help me why the recursive function does not work? If the first parameter is match the function returns proper value. If not, the return value is null.

function stockToDate($base_date, $i) {

  include "../lib/dbconn.php" ;     

  $today = date("Y-m-d") ;
  $base_date = date("Y-m-d", strtotime($base_date . "-1day")) ; 


  $q  = " SELECT txn_date "
      . "   FROM tworking_ymd   "  
      . "  WHERE txn_date = '$base_date' " ;
  $r = mysqli_query($dbc, $q) ;

  if(mysqli_num_rows($r) == 1){

    $row = mysqli_fetch_row($r);    
    return $row[0] ;

  } else {


    $i = $i + 1 ;
    if($i > 10) {
        return $today ;

    } else {

        stockToDate($base_date, $i) ;       
    }    
  }  
}
suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20
sky
  • 29
  • 8

1 Answers1

0

Each time your function calls itself, it needs to be able to pass the return value up the call stack. you miss return

function stockToDate($base_date, $i) {

  include "../lib/dbconn.php" ;     

  $today = date("Y-m-d") ;
  $base_date = date("Y-m-d", strtotime($base_date . "-1day")) ; 


  $q  = " SELECT txn_date "
      . "   FROM tworking_ymd   "  
      . "  WHERE txn_date = '$base_date' " ;
  $r = mysqli_query($dbc, $q) ;

  if(mysqli_num_rows($r) == 1){

    $row = mysqli_fetch_row($r);    
    return $row[0] ;

  } else {


    $i = $i + 1 ;
    if($i > 10) {
        return $today ;

    } else {

        return stockToDate($base_date, $i) ;       
    }    
  }  
}
suresh bambhaniya
  • 1,687
  • 1
  • 10
  • 20