Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
If you attempt to loop through the results of a select query a second time, it won’t work unless you reset the result object. Doing so is trivially easy in MySQL. This is mysqli__reuse.php which you can find in the Chapter 5, 05_05 folder of the exercise files. The try block at the top of the page submits a select query, and then stores the result set as result. In the body of the page, the while loop on lines 24 to 26 uses the fetch_assoc method to display an ordered list of links.
Further down, on lines 30 to 33, a second while loop attempts to use the same result object to display a definition list. So, if we load this into a browser, we can see what happens. The first list is displayed but the definition list isn't. This is because a result set uses an internal pointer to keep track of the next row. Displaying the first list puts the pointer at the end of the results set, so before trying to use the results again, we need to move the pointer back to the beginning. If you watched the first half of this course, you'll have learned that MySQL doesn't have a scrollable cursor. But it does have a very simple way of moving the pointer back to the beginning of a result set. So, let's do that. Before attempting to use the result set again we need to reset pointer, so add in a couple of lines there and then we need the results object. And we call it's data_seek method. And this takes a single argument, which is the position that you want to move the pointer to. And zero is the first position. So, if you've passed zero, you will move it back to the beginning of the result set. So, if we save that and refresh the browser. The definition list is now displayed correctly. You can also move the pointer to a specific record if you know its position in the results set, by using that number as the argument to data_seek.
The numbers are counted from zero. So, let's say you want to display in the definition list from Jack onwards. Jack is number five in our ordered list. So if we count from zero, that will be position four. So let's go back to the editing program and change that to four. Save the page and refresh the browser. And we're reusing the results set from Jack. So Jack is displayed all the way down to Sophia. And everybody else is missing. So, to reuse a result set, all you need to do is to pull the data_seek method on the mysqli_result object. Let's just take a quick look at that. It takes a single argument, which is the position that you want to move it to. If you want to move it to a specific row, use that particular number. But the rows are counted from zero, so setting data_seek to zero rewinds the result set for reuse.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 104390 Viewers
56 Video lessons · 116294 Viewers
71 Video lessons · 85543 Viewers
131 Video lessons · 40947 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.