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.
We'll connecting to MySQL in many different files throughout the rest of this course. So it makes sense to create the connection in an external file. If you haven't already done so, create a folder called includes at the top level of your testing site. We need to create a PHP file inside that folder. And save it as mysqli_connect.php. The file should only have an opening PHP tag.
We'll connecting using the object-oriented interface with MySQL Improved. So we need to create an instance of the MySQLi class and we'll store that as db. So that will be our database connector. Normally you only need four arguments for the class constructor, the address of the MySQL server, the username, password, and database name. So, we're connecting on Local Host. The username, O-O-P-H-P.
The password is lynda. And the Database is called O-O-P-H-P. If you're connecting to MySQL on a non-standard port, you need to add the port number as the fifth argument. For example, if you're using the default MAMP ports on a Mac, you'll need to change this. Let's just duplicate that line and you would add in a fifth argument, 8889. That's the default port for MySQL on MAMP on a mac.
I'm connecting on the standard port so I'll comment out that line. Next we need to check whether we've connected successfully. And if there is been a problem, the connection object will have a property called connect error. So, we can use that test whether we are okay. So if and then the db connector, the arrow operator, and connect error. And we can assign that to a variable called error. Notice that this doesn't have any parentheses at the end. It's a property of the connection object, it's not a method. That's all we need to connect to MySQL. So if we save that page, we can now test our connection. Open mysqli_test.php. You can find that in the ch05 > 05_01 folder of the exercise files. It just contains a little PHP block here that checks whether $error has been set. If it has, it shows you the error message, otherwise it tells you the connection has been successful.
We need to connect. Let's do that above the DOCTYPE. So insert a few lines there and create a php block. And inside that, we need to create a try_catch block because we're using a class that might throw an exception. So try and, after the closing brace, catch. Inside parentheses we're catching an exception. We'll call it $e, and if we do catch an exception we'll assign the message, the error message, to our error variable.
So we use the getMessage method. The code that we want to execute needs to go into the try block. All we're doing at the moment is going to include our database connection script. So require once. And the database connection script is in the includes folder. So we need to go up one two levels to be able to get to the includes folder. So that's ../../includes, and then the name of the file which was MySQLi_connect.php.
Just before testing this, I'm going to add one extra line. I'm going to add it after the PHP conditional statement, and it's db, that's our database connector. And then use the close method. And this will explicitly close the connection to the MySQL Server. If you don't do it explicitly, PHP will do it for you. So, the sky won't fall in if you leave out this line, but it's simple enough to do and it keeps everything tidy. So let's save that page and test it in the browser and that's great. We've got connection successful. I hope that's what you've got too. But let's just introduce a artificial error. Let's change the password. Save that. And refresh the browser. We get these horrible messages up here, but what we're looking for is the error message that we've got down here.
Access denied for user oophp@localhost using password YES. Well, if we're using the password and when access has been denied, it obviously means that the password is wrong. You'll need to check out whatever message you've got down here. One thing that might happen is, if localhost doesn't work, try using 127.0.0.1. That's the alternative for localhost. Let's just go back and correct that error.
Save it. Make sure that we're still working. And now that we've connected successfully to MySQL, we're ready to continue with the rest of the course.
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.