Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the last chapter, we learned the basic commands of MySQL. Now we're ready to learn how to use PHP to issue those commands. PHP offers three different ways to connect to MySQL. Database API is the technical term for each of these connection options. So, let's review these three Database APIs so that we can decide which one we're going to use. The first one is going to be just called MySQL. If the original MySQL API for PHP. The second one is MySQL I. The i stands for improved. It just has some improvements over MySQL.
It's very, very similar. And the third option is PDO. Which stands for PHP Data Objects. Now, all three of these are extremely similar. And once you learn how to use one, you can easily switch to another. The differences are kind of like the differences between driving different brands of cars. If you're driving a Honda when you're used to driving a Toyota. The controls for the windshield wipers and the headlights may be in different places. But overall, the concepts are the same. And with just a few minutes, you can make the switch. But there are a few key differences that are worth discussing. Here's a comparison chart that we can use to look at the differences between the three different APIs.
You'll notice that the first line is introduced, and you'll notice that MySQL was introduce very early on in PHP. It was the only API for version 2, 3 and 4 of PHP. But then in verision 5 and in 5.1, we got these new versions. So, right there, it give you a clue that MySQLi and PDO came later, and presumably offers some improvements and some advances. Over the original MySQL, which was there from a very early point. And sure enough, if you look at the next line, you'll notice that in version 5.5 of PHP, MySQL is going to be deprecated. That doesn't mean that it's going to go away.
It means that they're going to send out notification to everyone that it's going to be going away. Which means probably that in version 5.5 is the last time that we'll see it. As I'm recording this, version 5.4 is the current version. So it still works, we could still use it. But we might as well go ahead and be forward thinking and start using one of the other two APIs. All three APIs are included with PHP, only the first two though, MySQL and MySQLi are preconfigured for MySQL. And you might understand why if you look at the next line, and see the PDO supports other databases as well. So, that's a key difference between these.
PDO works with any database. So, if you're planning on using something like Postgres, well, you're going to want to use PDO. Because MySQL and MySQLi are only for MySQL. And since we're using MySQL that's fine, we can choose any of these stuff. But if your plan is to eventually use a different database, you'll want investigate PDO. Configuring PDO to work with MySQL is pretty simple. You just add a couple of lines to your php.ini file. Essentially telling PDO hey, I'm going to be using MySQL, so load up your MySQL code and get ready for it.
The next two rows tell us whether or not the API has a procedural, or an object-oriented interface. Now, we haven't been doing object oriented PHP yet, we've been doing procedural PHP. Object-oriented PHP is something that we talk about in PHP Beyond the Basics. Where we learn how to create classes and to put functions inside those classes. So, we've been doing procedural. So that's a big reason to prefer MySQLi over PDO, for that reason, because PDO is only object-oriented. MySQLi offers us both. Now in truth, they're all very similar.
The procedural any object-oriented are very similar and I'll show you that in just a second. First, I want to look at the last line there which notice that both MySQLi and PDO have support for prepared statements. We'll talk about this a little more later on, but it's a key feature that you want to be looking for in a database API when working with PHP. So, we're going to be using MySQLi. I don't want to complicate things by diving into object-oriented programming at this point. At the same time, it's going to be easy to switch between the two. Let me show you a side-by-side comparison of MySQLi with both the procedural and the object-oriented versions of the basic functions that we're going to be using.
So, there's a little bit of preview of what's coming up. On the procedural side, you can see all the functions that we're going to be using to work with our database. This is the API, these are the functions we'll call to do things with MySQL. On the right side, you'll see the object-oriented version. And you'll notice that they're almost identical. The only difference is that at the very beginning. We're creating a new object and that all of the functions that we're calling are inside that object. They're not functions at the root of PHP. But it's really that easy to switch between the two. The syntax is that similar. So, I don't want you to thing that because we're choosing procedural, that somehow we're ending up at a dead end.
You could easily convert over to object-oriented at any point and time later. And last of all, I just want to show you that if you want to find out more information about choosing APIs. The php.net website has more information for you. So, it's a lot of the same stuff that I showed you, side-by-side comparison of the different APIs. But there's a little more information there if you want to find out more. And now that we've picked MySQLi as our database API, let's see how we can use it with PHP to connect to MySQL.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101856 Viewers
61 Video lessons · 88609 Viewers
71 Video lessons · 72422 Viewers
56 Video lessons · 104123 Viewers
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.
Your file was successfully uploaded.