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.
PHP has a large number of database extensions. At the time of recording, this page in the online documentation shows there are four abstraction layers and 23 vendor specific database extensions. The vendor specific extensions use dedicated functions that interact only with a particular type of database. For example, Microsoft SQL server, MySQL or Post SQL. So, if you create an application with one of these extensions and later change your mind about the database you're going to use, you have to rewrite all of your code.
The idea of an abstraction layer on the other hand is to make your code more portable. Instead we've working with just one database, it'll work with several, as long as the PHP server has the correct libraries or drivers installed. In this course, we are going to look at one abstraction layer, PDO, which stands for PHP Data Objects. And at the object oriented version of the vendor specific database extension from my SQL. But why choose those two? When among the obstruction layers, PDO is the only one that's installed by default in PHP.
It's also the most versatile. Let's take a look at the list of PDO drivers and the databases they support. They include all the most important databases, including Microsoft SQL Server, PostgreSQL and MySQL. However, it's important to realize that the drivers need to be installed separately in PHP. Although PDO is installed by default, the drivers available on individual servers depend on how the server has been configured. You need to run phpinfo on your server to check which drivers are installed.
Let's take a look at the configuration set up on my local testing server. Here is PDO and pdo_mysql and sqlite, but nothing else. On Windows, extra drivers can be enabled by loading the appropriate dll file with php.ini but on Mac and Linux, they need to be compiled into PHP. Setting up PDO drivers is beyond the scope of this course. Speak to your server administrator or hosting company if you need help in doing so.
The vendor specific database extension covered in this course is MySQL Improved, or MySQLi. I've chosen it because MySQL is still by far the most widely used database with PHP. Although PDO supports MySQL, some features are easier to implement with MySQLi because the extension is designed specifically to work with MySQL. Code written using MySQLi will also work with MariaDB, the open source database that some Linux distributions have adopted as a replacement for MySQL.
Perhaps one of the most important considerations is that the original MySQL functions have been deprecated since PHP 5.5. That means functions such as mysql_query and mysql_fetch_assoc will eventually be removed from PHP. If your scripts still use them, you need to replace them either with PDO, or MySQLi, before they stop working. MySQLi has two interfaces. Procedural and object-oriented.
There's no significant difference in performance between the two interfaces. The procedural version was mainly designed to simplify the transition from the old MySQL functions. However, this course we'll use the object-oriented interface for two reasons. The code is more concise, so it involves less typing. And in my opinion, the shorter code makes it easier to read.
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.