Hello, in this video, I am going to cover connecting to a database. So we've got a database created, and how do we actually well connect to it. So it's pretty simple. We need PHP code. php and we'll need to create a server name. And this is going to be equal to localhost just because we are on a local host.
If we go here, PHP myadmin your server may name may be different will most likely will be different. If you are on a actual web server. If you can't find it, you know, ask the host provide you know jump on the child pop them a message and ask them how you know where can you locate it Where can you locate the server name server name, then we need a username if the active you log in and then we need to talk So the human is going to be this year, so whatever's here, so we'll be using that on a popular. And the password for this by default on local host, you know, just locally on exam is nothing. Obviously it's not secure. But you know, for locally local purposes, that's okay on a web server, definitely recommend having a password, most of them probably won't even let you do it without a password anyway, create a user, so you'll have to have a password.
So you'll probably be your web host, you know, login password, but you can create a different user, and you can view the users over. Okay, yeah, so if you go to user accounts, I can see them right here. So we're using this one, this one which has all privileges local host, and as you can see no password, you could add a user account privileges add a password, but I'm happy for you know, without it, it's all set up and good. So if you do have a password, you can put it in. What you want to do is a stripe statement. And this is basically going to check if the connection is successful because it potentially might not be.
And we're going to say connection equals new PDO. And we need to say, my SQL colon host equals dollar server name. And you'll just get it from here. So you just change it here and you'll get updated here. Then we're going to put a semicolon DB name equals and then you need to specify the actual database name which is what you created here. So epic database, epic database and And then the next few parameters are the username and password.
So username, password. And now what we want to do is basically set the PDO error mode to exception. So it will you know, potentially show us some errors if we get them to say dollar connection and this set attribute say PDO colon colon at tr, underscore no this error mode, then we are going to put a comma say PDO colon colon error mode underscore exception so you're actually throw an error, you know if it occurred and we're gonna say echo connect you successfully so if it gets to here is connected successfully so connected successfully. And obviously you can remove the face once you know you've got it working and all connected. And if it doesn't get it, you want to catch the exception and exception that we're going to catch the PDO PDO exception and then dollar error. And here we would just say echo connection failed.
And then we would just say dollar error. There exists a function we can get a message from so we can, you know, find out what the problem is. So, down here, if I run that, let's actually see what we get. So it's a connection file access deny for you the local host to database epic database. So apparently doesn't have access to it. So low closed.
Okay, there we go. So I was doing the wrong username should have just been root, my little bad right there and there we go. So if I put something in the password, which is invalid, we'll get a problem because it's denied because we're using the password even though it doesn't exist. If I try and access a database that doesn't exist which is epic ggx database does not exist. He we get a problem. So he says unknown database because we don't have that database.
And there we go. We have successfully connected to the database and over In the next few videos, we'll be looking at inserting data, retrieving the data and really, really cool stuff. So if you have any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next video.