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

3
  • 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? Commented Nov 15, 2016 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? Commented Nov 15, 2016 at 15:47
  • Assuming use of PDO here's a link to an example: stackoverflow.com/questions/1756743/… Commented Nov 15, 2016 at 15:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.