0

获取记录集后,我想做两件事。从第一条记录中打印名字和姓氏(所有记录都将具有相同的名称),然后在该记录下输出表中该记录集的所有记录。获取第一条记录的行为将指针向上移动一条记录,因此这不起作用。我想在从第一条记录中获取名字和姓氏后重置指针。我正在使用 PDO,除了我所阅读的内容之外,不推荐使用mysql_data_seek

这是我目前的做法(可能效率低下,因为我所做的只是重新运行相同的查询以获取完整的记录集 - 或者可以吗?):

   $query = "SELECT students.*, lessons.lessonname, assignments.assignmentid, assignments.complete, assignments.score    
            FROM lessons INNER JOIN 
            (assignments INNER JOIN students 
            ON assignments.studentid = students.id) 
            ON lessons.lessonid = assignments.lesson
            WHERE (((students.id)=".$_POST['viewstudentdrop']."))";
    $results = $pdo->query($query);
    $row = $results->fetch(); //just get the first record.
    echo 'Assignments for: ' . $row['firstname'] . " " . $row['lastname']. "<br>";        
    $results = $pdodl->query($query); //resets pointer
4

1 回答 1

1
$results = $pdo->query($query);
$data = $results->fetchAll();
$row  = reset($data);
echo 'Assignments for: ' . $row['firstname'] . " " . $row['lastname']. "<br>";        
foreach ($data as $row) {
...
于 2013-02-13T18:46:51.083 回答