SQL / PHP PDO Select random row

2019-07-29 07:43发布

I'm new to 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

标签: php pdo mysqli
1条回答
神经病院院长
2楼-- · 2019-07-29 08:04

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);
查看更多
登录 后发表回答