Accessing Databases with Object-Oriented PHP
Illustration by Don Barnett

Buffered and unbuffered queries


From:

Accessing Databases with Object-Oriented PHP

with David Powers

Video: Buffered and unbuffered queries

PHP treats MySQLi select queries as either buffered or unbuffered. Understanding this important concept will help you avoid errors when working with MySQLi. So what's the difference? With a buffered query, the result set is transferred immediately from the database server, and it's stored in PHP memory. By contrast, the results of an unbuffered query remain on the database server. And they're not fetched until required. Both have their advantages and disadvantages.
Expand all | Collapse all
  1. 13m 33s
    1. Welcome
      1m 4s
    2. What you should know before watching this course
      2m 8s
    3. Using the exercise files
      4m 56s
    4. Setting SQLite permissions
      1m 11s
    5. A quick primer on using PHP objects
      4m 14s
  2. 10m 12s
    1. Overview of PHP database APIs
      4m 5s
    2. Using prepared statements
      4m 24s
    3. Using transactions
      1m 43s
  3. 48m 57s
    1. Creating a database source name
      2m 3s
    2. Connecting to a database with PDO
      7m 27s
    3. Looping directly over a SELECT query
      3m 49s
    4. Fetching a result set
      8m 3s
    5. Finding the number of results from a SELECT query
      7m 14s
    6. Checking if a SELECT query contains results
      3m 32s
    7. Executing simple non-SELECT queries
      6m 2s
    8. Getting error messages
      7m 17s
    9. Using the quote() method to sanitize user input
      3m 30s
  4. 39m 51s
    1. Binding input and output values
      2m 36s
    2. Using named parameters
      9m 51s
    3. Using question marks as anonymous placeholders
      2m 35s
    4. Passing an array of values to the execute() method
      5m 20s
    5. Binding results to variables
      7m 53s
    6. Executing a transaction
      6m 54s
    7. Closing the cursor before running another query
      4m 42s
  5. 21m 20s
    1. Generating an array from a pair of columns
      2m 44s
    2. Setting an existing object's properties with a database result
      4m 42s
    3. Creating an instance of a specific class with a database result
      6m 1s
    4. Reusing a result set
      7m 53s
  6. 38m 14s
    1. Connecting to a database with MySQLi
      5m 57s
    2. Setting the character set
      1m 57s
    3. Submitting a SELECT query and getting the number of results
      4m 4s
    4. Fetching the result
      7m 35s
    5. Rewinding the result for reuse
      3m 20s
    6. Handling non-SELECT queries
      5m 27s
    7. Getting error messages
      5m 47s
    8. Sanitizing user input with real_escape_string()
      4m 7s
  7. 27m 49s
    1. Initializing and preparing a statement
      4m 17s
    2. Binding parameters and executing a prepared statement
      5m 55s
    3. Binding output variables
      5m 6s
    4. Executing a MySQLi transaction
      7m 5s
    5. Dealing with "commands out of sync" in prepared statements
      5m 26s
  8. 24m 7s
    1. Buffered and unbuffered queries
      4m 19s
    2. Using real_query()
      6m 1s
    3. Freeing resources that are no longer needed
      2m 31s
    4. Submitting multiple queries
      6m 41s
    5. Creating an instance of a class from a result set
      4m 35s
  9. 3m 31s
    1. PDO and MySQLi compared
      3m 31s

Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
please wait ...
Watch the Online Video Course Accessing Databases with Object-Oriented PHP
3h 47m Intermediate Jul 07, 2014

Viewers: in countries Watching now:

Now that PHP has true object-oriented capabilities, it's best practice to access databases using PDO (PHP Data Objects) and MySQLi. These methods produce database-neutral code that works with over a dozen systems, including MySQL, SQL Server, PostgreSQL, and SQLite. Learn how to use PDO and MySQLi to perform basic select, insert, update, and delete operations; improve security with prepared statements; and use transactions to execute multiple queries simultaneously. Author David Powers also covers advanced topics like instantiating custom objects, and compares PDO to MySQLi so you can decide which method is right for you.

Topics include:
  • Connecting to a database with PDO or MySQLi
  • Fetching a result set
  • Executing simple non-SELECT queries
  • Sanitizing user input
  • Binding input and output values
  • Passing an array of values to the execute() method
  • Working with advanced PDO fetch methods
  • Executing a MySQLi transaction
  • Freeing resources that are no longer needed
  • Submitting multiple queries
  • Creating an instance of a class from a result set
Subject:
Developer
Software:
PHP
Author:
David Powers

Buffered and unbuffered queries

PHP treats MySQLi select queries as either buffered or unbuffered. Understanding this important concept will help you avoid errors when working with MySQLi. So what's the difference? With a buffered query, the result set is transferred immediately from the database server, and it's stored in PHP memory. By contrast, the results of an unbuffered query remain on the database server. And they're not fetched until required. Both have their advantages and disadvantages.

Let's start with the advantages of a buffered query. Because it's in the PHP memory, you can get the number of rows in the result set, from the num_rows property. The result's set internal pointer, can be moved using the data_seek method, allowing you to reuse the result set. And storing the results in PHP's memory, means that further queries can be issued on the same database connection. The main disadvantage, is that the larger the number of results, the more memory is consumed. Not surprisingly, the pros and cons of an unbuffered query are the opposite.

The main advantage is leaving the results set on the database server until its needed, consumes less PHP memory. The disadvantages, are that you can't find out how many rows are in the results set. Nor can you use data seek, to move the pointer, or reuse the results. An unbuffered query, blocks further queries from being executed on the same connection. You have to wait until the end of the result set, or truncate it. It also increases the load on the database server. So how do you know which type of query is being executed? With non prepared statements, by default, the query method immediately returns a result set.

In other words, it's buffered. Although you can alter this default behavior, the more normal way to execute an unbuffered query, is to use the real_query method. This leaves the result set on the database server. If you previously used the original PHP MySQL extension, real query is the equivalent of the old MySQL unbuffered query function. Working with real query is a two stage operation. To retrieve the results, you need to call the use_result or store_result method.

However, store_result, converts it from an unbuffered query, to a buffered one. So in practice, use_result is the one you need. With a prepared statement, the default state after calling the execute method, is an unbuffered result set. That means you can't immediately use the num_rows property or data_seek method. Nor can you execute other queries if any results are still unused. However, all you need to do is call the store_result method on the statement object.

This buffers the result, allowing further queries to be executed and giving access to num_rows and data_seek. The alternative is to use the_get result method, to assign the result set to a MySQLI result object. This is, slightly less portable than store result. Because it uses the MySQL native driver, My SQL ND. This became available in PHP 5.3, but it didn't become the default driver until PHP 5.4.

All this can appear rather confusing. So, let's cut through the complexity, with a few simple guidelines. Buffered queries are easier to handle. So, I suggest you use to them in preference to unbuffered ones, most if not all of the time. This means using the query method, for non-prepared statements. With prepared statements, the decision isn't so clear cut. If the result is going to be consumed immediately, there's no need to buffer it. But if you need to access num_rows, data_seek, or further queries, use store result to convert it to a buffered query. Use unbuffered queries for very large result sets. And release resources, as soon as they're no longer needed, such as by closing a prepared statement as we did in the previous chapter. We'll look at other ways to free resources, later in this chapter.

There are currently no FAQs about Accessing Databases with Object-Oriented PHP.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Accessing Databases with Object-Oriented PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.