Jump to content

recursive function of select query does not always return valid result


momukhtar

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.