11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 35 • PRACTICAL DATABASE QUERIESwww.it-ebooks.infoCursor BasicsBefore moving on to how <strong>MySQL</strong> cursors are created and used, take a moment to review some basicsregarding this feature. Generally speaking, the lifecycle of a <strong>MySQL</strong> cursor must proceed in this order:1. Declare the cursor with the DECLARE statement.2. Open the cursor with the OPEN statement.3. Fetch data from the cursor with the FETCH statement.4. Close the cursor with the CLOSE statement.Also, when using cursors you’ll need to keep the following restrictions in mind:• Server-side: Some database servers can run both server-side and client-sidecursors. Server-side cursors are managed from within the database, whereasclient-side cursors can be requested by and controlled within an applicationexternal to the database. <strong>MySQL</strong> supports only server-side cursors.• Read-only: Cursors can be readable and writable. Read-only cursors can read datafrom the database, whereas write cursors can update the data pointed to by thecursor. <strong>MySQL</strong> supports only read-only cursors.• Asensitive: Cursors can be either asensitive or insensitive. Asensitive cursorsreference the actual data found in the database, whereas insensitive cursors referto a temporary copy of the data that was made at the time of cursor creation.<strong>MySQL</strong> supports only asensitive cursors.• Forward-only: Advanced cursor implementations can traverse data sets bothbackward and forward, skip over records, and perform a variety of othernavigational tasks. At present, <strong>MySQL</strong> cursors are forward-only, meaning that youcan traverse the data set in the forward direction only. Furthermore, <strong>MySQL</strong>cursors can move forward only one record at a time.Creating a CursorBefore you can use a cursor, you must create (declare) it using the DECLARE statement. This declarationspecifies the cursor’s name and the data it will work with. Its prototype follows:DECLARE cursor_name CURSOR FOR select_statementFor example, to declare the bonus-calculation cursor discussed earlier in this section, execute thefollowing declaration:DECLARE calc_bonus CURSOR FOR SELECT id, salary, commission FROM employees;After you declare the cursor, you must open it to use it.688

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!