Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We'll connecting to MySQL in many different connection object will have a property called connect error. files throughout the rest of this course.
So, we can use that test whether we are okay. So it makes sense to create the connection in an external file. If you haven't already done so, create a folder So if and then the db connector, the arrow operator, and connect error. called includes at the top level of your testing site. And we can assign that to a variable called error. We need to create a PHP file inside that folder. Notice that this doesn't have any parentheses at the end. 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. It's a property of the connection object, it's not a method. 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. That's all we need to connect to MySQL. 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 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.
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.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101498 Viewers
61 Video lessons · 88258 Viewers
71 Video lessons · 72129 Viewers
56 Video lessons · 103894 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.