‫Okay.
‫So now we're going to start to work with our database.
‫We have a blog database or blog schema with a post table.
‫We have some data in it.
‫We have a user.
‫So now we can start to work with that inside PHP.
‫Now we're not starting our job listings application yet.
‫We're just dealing with this blog kind of just just for learning purposes.
‫We're not building an extensive project or anything like that.
‫It's just to get you familiar with PDO and how things work.
‫So let's just open up index.php, right?
‫Which is completely empty.
‫And the first thing we want to do is create a new instance, a new PDO instance.
‫Remember we talked about object oriented programming.
‫And you create a class and then you instantiate a new object from that class.
‫Well PDO is an object that we want to instantiate.
‫Now there are some things that we need to pass in.
‫There's something called a DSN which is stands for data source name.
‫And this includes it's basically a string that includes the information required to connect to the database.
‫So this includes the driver.
‫So whether it's MySQL or Postgres et cetera.
‫The host the database name etcetera.
‫Now I like to put everything in variables that we're going to pass into this DSN.
‫So let's start at the top here.
‫And let's say.
‫Host, and we'll set that to local host.
‫Let's do the port.
‫And the default port is going to be 3306 for MySQL.
‫The DB name.
‫So we'll say db name.
‫Which in this case is going to be blog and then the username.
‫So you could either use your root user or one of the users you created.
‫I'm going to use Brad and then password.
‫For?
‫For my user is just one through six.
‫Okay.
‫So those are the database credentials let's say database creds.
‫Okay.
‫So next I want to create the DSN string.
‫So let's create a variable called DSN.
‫And we're going to set that to some double quotes.
‫And the way this works is we put the driver which is going to be my SQL.
‫And then colon, and then the host equals whatever the host is, which we have in a variable.
‫So I'm going to use this syntax the curly braces and then the host.
‫Now we separate these with with semicolons.
‫So I'm going to put a semicolon here.
‫Then we want the port.
‫And the port is going to equal the port from the variable.
‫Let's close that up.
‫Then we put a semicolon.
‫Then we have the DB name.
‫So it's going to be db name all lowercase.
‫And then we're going to set that to our variable of db name.
‫And then we also have the character set.
‫So we can say char set.
‫And you can put this in a variable if you want, but I'm just going to just hardcode it in here.
‫So it's going to be UTF eight.
‫All right.
‫And notice that the username and password do not go in the DSN.
‫We pass those in separately into the the PDO instance.
‫Now we're going to wrap everything in a try catch block because there can be you know if there's if
‫you can't connect then it'll throw an error.
‫And we want to be able to catch that error.
‫So we're going to say try and then catch.
‫Now in the catch.
‫This is going to take in an possible exception, which we'll call E, and we just want to right before
‫it just say PDO exception.
‫And in the try block.
‫This is where we're going to let's say create our PDO instance.
‫So we'll create a variable called PDO.
‫And we're going to set that to new because we're instantiating a new PDO object.
‫And this PDO object takes in three things.
‫In fact if we hover over it.
‫Uh.
‫You'll see.
‫It takes in the DSN.
‫A username and the password, I should say for things.
‫It can take in some options as well.
‫So let's pass into this our DSN.
‫And then we want to pass in our user name.
‫And then we want to pass in our password.
‫All right.
‫Now.
‫If if there's an error, we want to then catch that error.
‫We want to display it.
‫So let's put a comment here and we'll say set PDO to throw.
‫Exceptions on error so that then we can handle it down here.
‫So the way we do that is we take that PDO object and we call set attribute.
‫So set attribute is just a method in the PDO class.
‫And we're going to pass in PDO.
‫And then double colon adder.
‫So ATR underscore error mode so error mode.
‫And then we also want to pass in PDO double colon error mode exception.
‫All right.
‫So now that we've done that, we can come down into the catch and we'll say if.
‫If there is.
‫An error.
‫Catch it here.
‫And what I want to do is just echo out and we'll say actually use single quotes.
‫We'll say connection.
‫Failed colon.
‫And then we're going to concatenate onto that.
‫That E variable will have a method called get message.
‫And we want to output that.
‫And then the only other thing I want to do is if it connects successfully then up here let's say echo
‫database.
‫Connected.
‫Just so we know.
‫You know, we know exactly what's going on.
‫So I'm going to save this and let's go ahead and reload.
‫And we see database connected.
‫So we're now connected to our blog database using this user.
‫And if I were to change something like let's do the wrong password and I reload we get connection failed
‫access denied for user Brad at localhost.
‫Okay.
‫If I change the name here, let's say Brad one which doesn't exist, then it says access denied for
‫user.
‫All right.
‫So if it's correct then we get database connected.
‫So it's as easy as that to connect using PDO.
‫Now in the next video we're going to start to select some data and display it on the screen.
‫Since we're now connected.