In this video we are going to add in the PHP required to connect to our database. We are gonna go through it line by line so we know what's happening and we are also gonna test that it's working correctly. Okay, so before we start saving anything to our database, the first thing that we're gonna need to do is create a database connection. So if we just update this, so before it was submitting to our newsletter example, let's submit to newsletter signup and we'll create a new PHP file called Newsletter Signup. Oh, let's copy that. New file newsletter signup php.
And I'm just gonna again, copy over magically some PHP. Now if we go back to the browser and open up local host 80 80 and go to PHP my admin. And if you remember, this is what we used to manage our database. And just to refresh our memory, we had a meat yogurt database that we added a newsletters table two. So if we go back to that PHP script that I just pasted in and just have a quick look at what's going on here. So we've got a variable here, which is DB for our database.
And what this is doing is creating a new database connection. PDO is what PHP uses to connect to the database. And this is the information that we need to pass in to create that database connection. So my SQL is the type of database that we are using. The host is the IP address of the database that we're connecting to. The database name is Meet Yogurt, which we saw over here in PHP.
My admin, we've got Meet Yogurt and root is the username. And we don't have a password set, so we just leave it as blank. So what this line here is trying to do is trying, is trying to connect to the database. And if that fails, it's gonna catch it here and output an error message for us. So if we come down here and maybe if we just print our, our, our database just so we can see what's going on, if we go back to our index php, check that, that's submitting to the signup, we'll save that, go over to our website to refresh the page. And let's see what happens when we submit it this time.
I thought that that might show us a bit more information about our database connection, but it looks like it doesn't. But let's just go back to VS code. And that's the line that we were seeing. So we know that a database connection is working because it's not displaying this error message. Now, with the connection info, most of the time the only parts that you're gonna need to change are the database name and we're using root in a blank password because that's just the default that exam uses. When you actually upload your project to a server, you're gonna have a different username and password, but we'll cover that later on.
So for now, let's just see what happens when we break it. So let's, um, leave the t off the end of our database name, go back to our website and refresh the page. And we can see that now we're not connecting and we are seeing the error message. Let's un comment this line so we can see a little bit more information about why we're not connecting. So it's telling us that unknown database 'cause it's s spelled wrong. So if you go back to VS code, so this is, um, catching the PDO exception from the database.
So it's saying that something went wrong. Let's, let's catch the error. And we are using a get message method on that error. So what we can do is get rid of that and that will show us a lot more information. And again, if we use our pret tags that we saw before. So if we echo out pre tag here and then we'll put one underneath too.
Go back to our website. So now we can see this was the message that we were seeing before about the unknown database. There's just some other info here that might be handy for you when you're trying to do some debugging. So we know that this is working when we put the T back on because we're not seeing this error message. And we'll leave this but commented out because if there was to be a problem on our website, we don't want our users to be able to see this. We just want 'em to see this message and give them the option to go back to the homepage.
Okay, now that we have our database connection, in the next video we'll take the information from our form and add that into our database.