我是PDO的新手。我希望能够随机选择一名没有参加考试的学生('N')并回复姓名和科目。我怎样才能做到这一点?
$query = $db->prepare('SELECT name FROM exams WHERE faced = ?');
$array = array('N');
$query->execute($array);
答案 0 :(得分:5)
您可以使用以下内容:
$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);