Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using real_query()

From: Accessing Databases with Object-Oriented PHP

Video: Using real_query()

In addition to the query method, MySQLi At the top of the page, there are two select queries And the first result is stored as result.

Using real_query()

In addition to the query method, MySQLi has the rather oddly named real query method. With the select statement, the query method executes the query and returns a MySQLi result object, with the result set stored in PHP memory, ready for use. The real query method on the other hand, simply submits the query. You need to retrieve the result set separately. This is mysqli_real_query.php, which you can find in the chapter 7 07_02 folder of the Exercise Files.

And we'll use this to experiment with the differences between query and real query. At the top of the page, there are two select queries that are executed one after the other using the query method. And the first result is stored as result. The second one as result2. Down in the page, two while loops display the different results. And if we load that page into a browser, we can see the two different result sets being displayed. So, let's change the first one to real query.

So we need to scroll up, and then on line five, we'll change query to real query. Save that. And then, reload the page in the browser. And we get this message. Error generated by second query. Commands out of sync, you can't run this command now. This message is coming from the second query, so there's obviously nothing wrong with the first one. So, let's comment out the second query and it's related error message and try again.

Let's go back to the code, comment out lines 9 to 12. Save and reload. This time we get fatal error. Called to a member function fetch a sock on a non-object on line 30. So, let's see what's happening on line 30. And line 30 is trying to use result to call the fetch a sock method. So if we go up here, result is coming from real query. But if that's not containing the results set, what does it contain? It's actually a boolean true or false.

It's actually a boolean true or false. When you use real_query, all that it returns is the boolean that tells you whether the query was executed successfully. To get the results set, you need to call another method on the database object. So let's remove result from there, and then on the next line, result equals then our database object, and we call the use_result method. So, let's save that and reload the browser.

This time we are getting our first result. The error messages that we've got down here, are related to result2, which is currently commented out. So, let's go back and un comment that, and see if it makes a difference. When we reload the browser, we've got that second error message again. Commands out of sync, you can't run this command now. What's going on is that real_query is unbuffered. The results from real_query are still on the database server. So, no further queries can be submitted until all the results have been consumed.

So, there are a couple of ways in which we can solve this problem. Let's go back to the code. And one way is to cut this second block. And make sure that it doesn't run until the first set of results has been consumed. So if we put a new PHP block after the first set of results is used, paste that in there. Save and reload the page. Everything is now being displayed correctly. We've consumed the first set of results up here, the unbuffered results. And only after that, did we try to execute the second query.

The alternative is to change use result to store result. So let's go back and try that. We'll select all this code here, cut it. We don't need that PHP block anymore. And move the second query back to its original position. And then change use result to store result. And if we save that, and reload the browser, both results are now being displayed. So, why does store result work but not use result? Well, let's go back.

Well, let's go back. Actually, what is happening is that we've now created a buffered query. These two lines together five and six have exactly the same effect as using the query method on its own. So most of the time, you'll want to use buffered queries. And for that, it's much simpler to use the query method. However, if you do have a very large results set, using an unbuffered query consumes less memory. For an unbuffered query use the real query method to submit the query, and then call the use result method on the database object to start using the results.

the database object to start using the results. But remember that unbuffered queries have several restrictions. You can't reuse the result set without submitting the query again. There's no way to find out the number of rows returned by the query, and you can't run any further queries until the unbuffered results have been fully consumed or you clear the connection. We'll be looking at three resources that are no longer needed next.

Show transcript

This video is part of

Image for Accessing Databases with Object-Oriented PHP
Accessing Databases with Object-Oriented PHP

47 video lessons · 2720 viewers

David Powers
Author

 
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 learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.

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 ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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.