momukhtar Posted September 21, 2008 Share Posted September 21, 2008 function getMcqQuestion() { $RandomQuestion=mt_rand(1,$_SESSION['MCQ_totalQuestions']); echo "qid ".$RandomQuestion. "<br/>"; $q_Question_mcq = "select question, choice_1, choice_2, choice_3, choice_4 from question_bank, question_choice where question_bank.question_id = question_choice.question_id and question_bank.question_id = $RandomQuestion"; $SqlQuestionSelected = mysql_query($q_Question_mcq ) or die ("Error... Cant select a valid question"); //echo $SqlQuestionSelected; $i = mysql_num_rows($SqlQuestionSelected); if ($i == 1) { $RowQuestionSelected = mysql_fetch_array($SqlQuestionSelected); echo"Inside Func Row <br/>"; print_r( $RowQuestionSelected ); return $RowQuestionSelected; } else { mysql_free_result($SqlQuestionSelected); $SqlQuestionSelected = ""; getMcqQuestion(); } } // ends function $RowQuestionSelected = getMcqQuestion(); echo"Row Outside function <br/>"; print_r( $RowQuestionSelected ); When in the first call i get the valid row then outside the function I get the valid resultset. Otherwise if there are two or more recursive calls inside the function then inside the function i get the valid row and outside the function i get an empty row. Quote Link to comment https://forums.phpfreaks.com/topic/125227-recursive-function-of-select-query-does-not-always-return-valid-result/ Share on other sites More sharing options...
sasa Posted September 22, 2008 Share Posted September 22, 2008 change else { mysql_free_result($SqlQuestionSelected); $SqlQuestionSelected = ""; getMcqQuestion(); } to else { mysql_free_result($SqlQuestionSelected); $SqlQuestionSelected = ""; return getMcqQuestion(); } Quote Link to comment https://forums.phpfreaks.com/topic/125227-recursive-function-of-select-query-does-not-always-return-valid-result/#findComment-647379 Share on other sites More sharing options...
momukhtar Posted September 22, 2008 Author Share Posted September 22, 2008 thanx Quote Link to comment https://forums.phpfreaks.com/topic/125227-recursive-function-of-select-query-does-not-always-return-valid-result/#findComment-647428 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.