Jump to content

recursive function of select query does not always return valid result


Recommended Posts

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.

change

else 
{
mysql_free_result($SqlQuestionSelected);
$SqlQuestionSelected = "";
getMcqQuestion();
}

to

else 
{
mysql_free_result($SqlQuestionSelected);
$SqlQuestionSelected = "";
return getMcqQuestion();
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.