20 | Connect to a Database From a Website Using PHP | 2023 | Learn PHP Full Course for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the last couple of episodes we talked about how to go inside a database and create a table as well as creating data inside those tables and updating them and deleting them you know just kind of like manipulating data inside a database but we haven't actually talked about how to do that from inside a website so that's what we're going to do today as you can see I have a very basic website in front of me there's not really anything special here we just basically have a index.php which doesn't really have anything there's nothing inside the body tag so what I want to do in here is I want to go ahead and actually create a connection to my database so I can actually go in and run PHP code that can actually query this database so in order to do that we need to have a connection going first so what I'm going to do is I'm going to go inside my project file and I'm going to create a new folder and I'm going to call this one includes now includes basically means that this is going to contain files that are not going to be seen inside the actual website so for example a pure PHP file that just needs to run a script in order to do something but it's not a actual file that we visibly see as a page you know in the same sense as we see this index.php file because that's the front page so the include files are just basically extra files that just run code the first thing I'm going to do inside this folder here is create a new file so I'm going to say I want to create a new file I'm going to call this one dbh dot Inc dot PHP which stands for databasehandler DOT includes dot PHP now it's very important to point out here that it is possible to go in and name it as dbh.inc which is also a kind of file that we can use and create PHP code inside of but this kind of file can create issues and some people when they hear that I I call it dot Inc dot PHP think that we are creating a DOT Inc file that's not what we're doing here the dot Inc is just a naming convention so to speak so it doesn't really do anything this is going to be a PHP file so in this case if we could call it dbh dot Inc or you could call it dbh Dash Inc or just db8 ink if you wanted to it's really the same thing because it's just a name so don't get confused about the naming convention that I'm using here it's just a way for us as the developer to know exactly what kind of file this is so what I'm going to do is I'm going to name this file and create it and inside this file I'm going to open up my PHP tag so we can actually create some PHP code and it's important to point out here because we talked about this in my syntax video at the very beginning of this course that when we have a pure PHP file we don't create a closing tag so for example you would create this at the end of a you know a pair of PHP tags we're not going to do that when it's a pure PHP file and the reason for that is if it were to accidentally go below here and create some HTML or something just by mistake then we can create more damage than you know not doing that again you can always go back and watch that episode at the beginning of the course if you want to know more about this but we're just not gonna put it in here okay um so going in here what we can do is we can start off by saying that we want to include some information about our database we did of course create a database in the past couple of episodes so if we were to go in here you can see that I created a database called my first database and inside this database we also created a couple of tables that could actually have some sort of information inside of it so we have a comments table and we have a users table where we actually do have some users just because we learned how to insert and you know update and delete data and that kind of thing so we have some stuff in here is what I'm trying to say all we have to know for now is that we have a database called my first database because we do have this PHP my admin hooked up to our servers so when we do actually need to connect to our database we do need to tell it which one of all these databases we're trying to connect to because it is possible to use more than just one database inside a website you can use multiple if you want to and there there is Arguments for doing that you know for different reasons but for most websites you're just going to have one database for everything so remember the name my first database so going back inside our file here and I'm going to create a DSN which stands for data source name which is US telling our server what kind of database driver we're trying to use and you know what is the database name what is the host that we're trying to connect to here in this case it's going to be localhost so we need to give it a little bit of information about this database we're trying to connect to so in our case we're using a mySQL database so we're going to create a variable I'm going to call this one DSN and I'm going to set it equal to a string and inside of this string here I'm going to say that I want to connect to a mySQL database driver then I want to tell it what kind of host I'm trying to connect to here in this case it's going to be localhost so I'm going to say equal to localhost semicolon and then I want to tell it the database name which in this case we did already just go in and check in my case it's called My first database and again if you call this something else in the past couple of episodes you do need to change this so it matches whatever you called your database so you basically just go in and change the information here depending on what database you're trying to connect to so underneath here I need to give it two more pieces of information I want to give it a database username so we're going to say DB username and this one is going to be equal to root and I do want to explain this in just a second but let's go ahead and create the next one as well so I'm going to create a database password and this one is going to be empty in my case here password there we go um so basically when we have a database we do also have a username and a password in order to connect to our database which makes sense so in my case because I'm using my exam software and I have not gone in and actually changed this the default username and password is going to be root and then no password I do want to point something out here though because if you're using a Mac computer you may need to go inside your password and write root because I did experience 12 years ago when I was studying my web development bachelor's degree that people who were using Mac computers and using xampp did X need to include root in both places because xampp is a little bit different on Mac than it is on Windows when it comes to at least this information here so if you're sitting on Mac and doing this right here doesn't work for you try writing route both places and see if that works for you or just Google how to change your password and username for your database again there's a couple of different options here we're just going to stick with this information for now and then I actually want to go down and run a try catch blog so we can actually see if we get some a pop-up here and it looks like this so basically we have a tri-cats block which means that we are basically running a block of code and if an error occurs then I can do something else by catching the error and then doing something with the error message that's basically what a try catch is and you'll see this very often inside PHP because a try catch block is very useful so what I'm going to do inside my try is I'm going to say that I want to run a PDO connection and we didn't actually talk about this yet because when it comes to connecting to databases we have three different ways we can do it we have what is called a MySQL connection which is very bad and you should never use that because it's obsolete and they actually came up with a new way to connect to a database which is called mysqli which stands for improved this basically goes in and does extra SQL injection prevention so don't use MySQL because there is some security things that is just not very good but now let's not talk more about ways that you cannot connect to a database because we have talked about that now but what you can do is you can connect to a database using mysqli or or we can also use the third method which is called PDO now PDO stands for PHP data objects which is basically another way for us to connect to a database that is a little bit more flexible when it comes to different types of databases out there mysqli is very good when it comes to mySQL databases but if you plan to connect to other types of databases for example SQL Lite or something then you can use something like PDO it has also been a thing in the past lessons of mine that people do request that I use PDO so we are just going to stick to using PDO since that is going to be the one that people lean more towards because it is more flexible but for people who are curious about what exactly the difference is when it comes to the actual programming when it comes to mysqli and PDO it's basically just the methods that that you know change a little bit when you when you start programming it if you are curious about mysqli you're more than welcome to look it up but we're going to be using PDO in these lessons here so having ranted a little bit about different ways to connect to a database we are now going to create a PDO connection so PHP data objects is a way for us to create a database object when we want to connect to a database so basically we turn the connection into a object that we can use inside our PHP code and just refer to that object whenever we want to connect to a database so what we're going to do is we are going to have a variable called PDO I'm going to go inside of it and create a new PDO object so we're going to say new PDO and what this basically does is that it instantiates a PDO object off of a existing class inside our PHP language that is going to create this connection based on a couple of parameters so for example what is the you know the database driver going to be what is the host gonna be what is the database name we're connecting to what is the username what is the password and then it's going to create a database connection object that we can use so going inside this PDF so I'm going to give it a couple of parameters the first one is going to be the DSN which we just created up here so we have all of this information then I'm going to give it the username so we're going to say DB username and then I'm going to give it the database password so doing this here we now have a database object and just to mention it here we could technically just take this one line of code and do this right here and that would actually be enough to connect to our database if all the information is correct every single time but we do want to have some sort of error handlers you know if an error happens then we want to be able to grab that error message and show it inside our website um so you know even though this is like the pure bare bone you know enough to connect to a database it is a good idea to run this try cats block here to you know get any sort of potential errors so what I want to do is I want to set a couple of attributes inside this Arctic that we created here we can do that by going in and say we want to grab this PDO variable that we just owned objects that we just created here because now it's no longer a variable it is actually a object and I want to point to a existing method inside this object called set attribute which is going to allow for us to change a couple of attributes about this particular object that we just created for example how do we want to handle error messages so we may run into when we try to connect to our database so inside the parameters here I can say I want to grab a specific attribute so in this case it's going to be PDO colon colon attr underscore error mode so error mod you can actually see it puffs up over here so we're going to grab the error mode and then we're going to say we want to set it to a PDO colon colon e r r m o d e underscore exception so right now we are saying that if we get a error then we want to throw a exception which means that we can go down inside our cat's block down here and actually grab that exception which is you know information about this error here so what I can do is I can say I want to catch my PDO exception and I want to name this one variable e so we're basically just saying that this is a PDO exception type which is going to be named as variable e which is a placeholder that we can refer to inside the curly brackets here so if an error message happens then I want to go in and Echo out a message connection failed colon space and then I want to concatenate a error message so right now we are grabbing the Exception by referring to variable e so I want to go after here and paste that in and say I want to run a method called get message parentheses and semicolon so right now we're getting the exception which is the error that may be thrown and then we want to grab the actual error message and Echo that out inside the browser so if we don't connect correctly to a database then just go ahead and throw an exception here but like I said this one line of code here is actually the one line that we use in order to actually connect to our database so all this other stuff down here is error handling and throwing you know error messages inside the screen if the connection fails that kind of thing so for now just know that this line here is the important one so now that we have this we can actually go in and actually do stuff inside our code so if I wanted to you know select data from a database or if I want to insert data inside my database I can do that by simply running this one connection here and actually query something into my database using PHP code and we haven't talked about how to do that yet but that is something we're going to talk about in the next upcoming lessons so for now we learn how to connect to our database and in the next upcoming lessons we're going to learn how to insert data we're going to learn how to update and delete data so we can actually use the connection for something so I hope you enjoyed this lesson and I'll see you guys next time foreign [Music]
Info
Channel: Dani Krossing
Views: 84,603
Rating: undefined out of 5
Keywords: php connect to database, how to connect to a database using php, connect to database using php, how to connect to a database from a website, connect to a database from a website using php, php database connection, php database connection using pdo, connect to database using pdo, connect to database using pdo and php, pdo connection to database using php, php tutorial, pdo tutorial, php pdo tutorial, learn php
Id: tHKsZdS8Oug
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Mon Jun 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.