24

Using Cursors

In this chapter, you’ll learn what cursors are and how to use them.

Understanding Cursors

Note

This Chapter Requires MySQL 5 Support for cursors was added to MySQL 5. Therefore, this chapter is applicable to MySQL 5 or later only.

As you have seen in previous chapters, MySQL retrieval operations work with sets of rows known as result sets. The rows returned are all the rows (zero or more of them) that match a SQL statement. Using simple SELECT statements, there is no way to get the first row, the next row, or the previous 10 rows, for example. There is also no way to process all rows one at a time (as opposed to processing all of them in a batch).

Sometimes there is a need to step through rows forward or backward and one ...

Get MySQL Crash Course, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.