‫Okay.
‫So now we're going to integrate our database with our application.
‫So we're going to connect through PDO which is PHP data objects.
‫And it's an extension to work with databases to connect make queries and so on.
‫And what we're going to do is have a database class in the the last section, not the last section,
‫but the section where we learned about databases and we built the little blog.
‫We just did procedural code coding.
‫We didn't have any database class or anything.
‫But when you're building a real application, if you're using just vanilla PHP, you're probably going
‫to create a class that you can create a database object from, and then you can call methods on that
‫object, different queries and so on.
‫So let's just for now we'll put this in the root.
‫Later on we're going to put all of our classes together in a framework folder.
‫But we'll we'll get to that later.
‫So let's create a new file in the root called database with a capital D dot PHP.
‫And this is going to be a class.
‫So let's say class database.
‫And we're going to have a property that represents the connection.
‫So let's create a public property called.
‫Con con and that's going to represent our connection.
‫Then we'll have our constructor.
‫And this is where we can pass in some config values to connect to our database.
‫So all we'll have to do is instantiate the database class or object to connect.
‫So I'm just going to put some comments here.
‫First let's say constructor.
‫For database class and program.
‫Is going to be an array called config okay.
‫So it's going to be the config values including things like the host, the port, the database name,
‫the username password, all that stuff.
‫So let's say public function double underscore construct and pass in config.
‫Okay.
‫Then down here we're going to have the DSN.
‫And if you remember from the the other tutorials, when we when we instantiate a PDO or PDO object,
‫we pass in the DSN.
‫And that's a string with the host port, database name, UTF encoding, etcetera.
‫So let's create a variable here.
‫We'll just say DSN.
‫And we're going to set that equal to some double quotes.
‫And in those quotes we're going to have the driver which is MySQL colon host equals.
‫And we're going to have our config array that's passed in which is going to have the host.
‫So we want to pass in host into some brackets.
‫All right.
‫Then we have a semicolon that's going to separate the different parameters here.
‫Then we have the port.
‫So the port is going to be equal to the config.
‫And we want to pass in here.
‫Port.
‫Okay after the port semicolon, then we have db name all lowercase.
‫We're going to set that to the config and in brackets we want to pass in db name.
‫I'm going to keep it lowercase as well just because just to match the the label here.
‫And then that should do it.
‫As far as the DSN.
‫Now remember when we set an option for to to throw exceptions on errors.
‫So we'll have a variable here called options.
‫And this will be an array of any options that we want to set in PDO.
‫And I want to set that that attribute error mode.
‫So we can do that by saying PDO double colon addr underscore error mode.
‫And we want to have that as PDO.
‫Error mode exception.
‫Okay.
‫Same same option we did in the last video where we did this.
‫And then we'll have a try catch.
‫Just like we did before.
‫So we're essentially doing the same thing.
‫We're just doing it inside of a constructor of a class.
‫And in the trie, what we're going to want to do is connect.
‫Now, since con is a is a property of the class, we have to use this, right.
‫So we'll say this con and we'll set that to a new PDO instance.
‫And we're going to pass in here that DSN.
‫We're going to pass in the username and password which are going to come from that config.
‫So we'll say config username.
‫And then.
‫Config.
‫Password.
‫Okay.
‫So those are the things we want to pass in.
‫And that will that will try to connect.
‫Now if something goes wrong then we're going to catch an exception.
‫So we can put some parentheses here and say PDO exception.
‫And then a variable that we'll call E and we want to throw, we'll say throw new exception.
‫And we'll just pass in a message that says database.
‫Connection failed and we'll do colon.
‫And then let's just add on to that.
‫Actually we have double quotes.
‫So we can just put it right in here.
‫So we'll put the E.
‫And then there's a method on that called get message.
‫And we'll show whatever that message is whether we forgot whether the password is wrong or whatever
‫the case is.
‫All right.
‫So let's save this.
‫And obviously it's not going to do anything yet because we just created the class.
‫We didn't instantiate it or anything like that.
‫Now we have to think about the config that's being passed in.
‫So what I want to do is have a config file that has those parameters in it.
‫So let's create a folder called config.
‫And then in that we'll create a db dot php file.
‫So this is just going to have our the parameters that we pass in.
‫So we're simply going to return.
‫Let's put our PHP tag in here.
‫We're going to return.
‫In array.
‫And we're going to have the host.
‫So the host is going to be local host.
‫The port.
‫Is going to be the default MySQL port, which is 3306.
‫Then we have the DB name.
‫So I called it work Obeah.
‫It's the same as the schema name that you created in workbench and then username.
‫So I'm going to use my Brad user.
‫Just make sure that you use whatever username you set up and then password.
‫Which I believe I used one through six.
‫Pretty sure.
‫Let me just test that out in the terminal real quick.
‫And this is how you can kind of test if, if you have your your user working or not.
‫So if we say MySQL you Brad.
‫123456.
‫And it lets me in.
‫Okay, good.
‫So that works.
‫So it should work in our application.
‫So let's save that file.
‫And then what we want to do is go into our public index.php which is basically our entry point.
‫And we're going to require the database file.
‫So let's go up here and say require.
‫And we'll say base path.
‫And it's in the route.
‫So it's just database dot PHP.
‫Okay.
‫Then we want to bring in our config.
‫So let's say config equals and we're going to require.
‫Oops.
‫We're going to require the whoops.
‫We want to use base path.
‫And that's going to be config slash db dot php.
‫Okay.
‫So we're bringing in the database class and the config file.
‫Now we just need to instantiate the database.
‫So let's call it db.
‫We'll set that to new database.
‫And pass in that config file, right?
‫Which just returns the array.
‫And that array is going to get passed in here.
‫And then plugged into all these parameters where it needs to go.
‫All right.
‫So let's save that and let's reload.
‫And we don't see anything which is a good sign.
‫So we could just test it.
‫We could just echo out here.
‫Connected.
‫And if I reload, we see connected.
‫Also, if you were to change anything in the config, like let's say Brad one, which is a user that
‫doesn't exist, and then I reload, we're going to get this fatal error.
‫Database connection failed.
‫Access denied for user Brad.
‫All right.
‫So I'm going to put that back.
‫And that's it.
‫Now we're connected.
‫So the next thing I want to do is in this database class, we're going to want a way to query our database.
‫So we're going to create a query method so that we can then move on to fetch all of our listings and
‫so on.