Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL / PHP PDO Select random row

Tags:

php

pdo

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

like image 739
NathaliaZeed Avatar asked Sep 08 '25 07:09

NathaliaZeed


1 Answers

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);
like image 160
ajtrichards Avatar answered Sep 11 '25 06:09

ajtrichards