Kaboom Kaboom - 17 days ago 6
MySQL Question

Generate a mysql query for each item in an array

I have an array with 3 values in it. These three values change depending on the user account (it's for referencing recovery questions by their id).

When they submit the form with their email address, the system checks it and if it exists gets their 3 recovery question id's which are store in a different database and the 3 answers from their account.

Then it stores the information into an array like so:

<?php
$questions = Array($question1id, $question2id, $question3id);
?>


How can I make a
foreach()
for this array that will query like so

Select * FROM recovery_questions WHERE rid='$val'


and return that for each question and store them in separate variables like
$cleanquestion1
,
$cleanquestion2
, and
$cleanquestion3
?

Every time I try I overwrite the last value and its got me frustrated. Thanks

Answer
<?php
$a = 1;
$questions = Array($question1id, $question2id, $question3id);
foreach ($questions as $v){
    $sel = "Select * FROM recovery_questions WHERE rid='$v'";
    $stmt = $db->query($sel);
    while($r = $stmt->fetch()){
          $qvar = "cleanquestion".$a;
          $qqvar = $<<results>>
    }
    $a++;
 }
 ?>

substitute results with whatever you get back from db.

then

  echo $cleanquestion1

will result with whatever result of question1id is.

Comments