Recursive function including mysql does not work in php [duplicate]












-2















This question already has an answer here:




  • Why doesn't this PHP recursive function return the value?

    4 answers




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) ;
}
}
}









share|improve this question















marked as duplicate by deceze mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 5:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • if (...) { return ... } else { NO RETURN HERE }
    – deceze
    Nov 13 '18 at 5:34


















-2















This question already has an answer here:




  • Why doesn't this PHP recursive function return the value?

    4 answers




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) ;
}
}
}









share|improve this question















marked as duplicate by deceze mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 5:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • if (...) { return ... } else { NO RETURN HERE }
    – deceze
    Nov 13 '18 at 5:34
















-2












-2








-2








This question already has an answer here:




  • Why doesn't this PHP recursive function return the value?

    4 answers




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) ;
}
}
}









share|improve this question
















This question already has an answer here:




  • Why doesn't this PHP recursive function return the value?

    4 answers




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) ;
}
}
}




This question already has an answer here:




  • Why doesn't this PHP recursive function return the value?

    4 answers








php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 5:31









suresh bambhaniya

863113




863113










asked Nov 13 '18 at 4:48









sky

116




116




marked as duplicate by deceze mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 5:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by deceze mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 13 '18 at 5:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • if (...) { return ... } else { NO RETURN HERE }
    – deceze
    Nov 13 '18 at 5:34




















  • if (...) { return ... } else { NO RETURN HERE }
    – deceze
    Nov 13 '18 at 5:34


















if (...) { return ... } else { NO RETURN HERE }
– deceze
Nov 13 '18 at 5:34






if (...) { return ... } else { NO RETURN HERE }
– deceze
Nov 13 '18 at 5:34














1 Answer
1






active

oldest

votes


















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) ;
}
}
}





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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) ;
    }
    }
    }





    share|improve this answer


























      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) ;
      }
      }
      }





      share|improve this answer
























        0












        0








        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) ;
        }
        }
        }





        share|improve this answer












        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) ;
        }
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 4:53









        suresh bambhaniya

        863113




        863113















            Popular posts from this blog

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly