在没有mysql_data_seek的情况下重置结果指针 [英] Resetting results pointer without mysql_data_seek
问题描述
获取记录集后,我想做两件事.从第一个记录打印名字和姓氏(所有记录将具有相同的名称),然后在该记录下,在表中输出该记录集的所有记录.提取第一条记录的操作会将指针向上移动到一条记录,因此将无法正常工作.我想在获得第一个&之后重置指针姓氏从第一条记录中删除.我正在使用PDO,除已阅读的内容外,不推荐使用 mysql_data_seek .
I want to do two things after fetching a recordset. Print the firstname and lastname from the first record (all records will have same name), then under that, output all of the records for that recordset in a table. The act of fetching the first record moves the pointer up one record, so that won't work. I want to reset the pointer after getting the first & last name off the first record. I'm using PDO, and, besides from what I have read, mysql_data_seek is deprecated.
这是我当前的操作方式(也许效率不高,因为我正在做的是重新运行同一查询以获取完整的记录集-可以吗?)
Here's how I'm currently doing it (inefficient perhaps, as all I am doing is rerunning the same query to get the complete recordset -or is it OK?):
$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
推荐答案
$results = $pdo->query($query);
$data = $results->fetchAll();
$row = reset($data);
echo 'Assignments for: ' . $row['firstname'] . " " . $row['lastname']. "<br>";
foreach ($data as $row) {
...
这篇关于在没有mysql_data_seek的情况下重置结果指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!