1

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

Kaboom
  • 674
  • 6
  • 27
  • 1
    Why not issue 1 statements that says Select * FROM recovery_questions `WHERE rid in ($question1id,$question2id,$question3ID) order by rid` so you get back all 3 in 1 call? – xQbert Nov 15 '16 at 15:45
  • That seems viable, but how would I call them separately? I don't often bundle my query's like that is why I ask. I imagine its something like $row[0], $row[1], etc? – Kaboom Nov 15 '16 at 15:47
  • Assuming use of PDO here's a link to an example: http://stackoverflow.com/questions/1756743/how-to-loop-through-a-mysql-result-set – xQbert Nov 15 '16 at 15:48

1 Answers1

1
<?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.

bart2puck
  • 2,432
  • 3
  • 27
  • 53