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.
Most of the time, PHP automatically frees up memory when resources are no longer needed. So it's common practice to leave the job of freeing resources to PHP's garbage collection. But with MySQLi there are times when you need to free resources explicitly, and in fact it's recommended you always do so. There are three methods that free up database resources in MySQLi, close, free and free_result. Let's take a look how they're used. With the MySQLi result object, all three have the same meaning.
They free the memory associated with the result set and destroy the result object. With a prepared statement calling the close method, destroys the statement object, and cancels any results that haven't yet been retrieved from the database. Because prepared statements aren't buffered by default, you need to call the Close method before attempting to submit another query if the result set hasn't been completely consumed. Alternatively, use the store _result method to buffer the result set. Calling the free_result method frees up the memory associated with a result set that's been buffered using store_result. Unlike close, it doesn't destroy the statement object, so you can use it to prepare and execute another prepared statement. Those close method also closes a database connection. That's how the close, free, and free result methods are used, but do you really need to bother? The one situation where it's vital to free resources is to avoid the commands out of sync error. This happens when you have unused results in an unbuffered query and want to issue another query. You need to free the resources associated with the unused results.
Alternatively, you must store any unbuffered results before issuing further queries. In all other circumstances, PHP automatically closes resources when the script ends. Nevertheless, the recommended best practice is to close MySQLi resources as soon as they're no longer needed. With short example scripts such as in this course, it makes little or no difference. But on a busy website, freeing memory and handing back control to the MySQLi server could improve efficiency. It's also a good idea to free the memory associated with multiple result sets as soon as they're no longer required.
There are currently no FAQs about Accessing Databases with Object-Oriented PHP.
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.