4

I want to be able to select a STUDENT randomly who has not FACED the exam ('N') and echo the name and subject. How can I achieve this?

$query = $db->prepare('SELECT name FROM exams WHERE faced = ?');
$array = array('N');
$query->execute($array);

enter image description here

1 Answer 1

9

You can use something like:

$query = $db->prepare('SELECT name, subject
          FROM exams WHERE faced = ?
          ORDER BY RAND() LIMIT 1');
$array = array('N');
$query->execute($array);

$result = $query->fetchAll(PDO::FETCH_COLUMN, 0);

var_dump($result);
Sign up to request clarification or add additional context in comments.

3 Comments

Just for the future: webtrenches.com/post.cfm/avoid-rand-in-mysql Try to avoid using this method, and maybe try something from the following post: stackoverflow.com/questions/4329396/…
Yes RAND() is a recipe for killing mysql server !!
If you have INDEX(faced) it won't be so bad. Even better would be a "covering" INDEX(faced, name, subject).

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.