36: How to connect to a database in PHP | PHP tutorial | Learn PHP programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the previous episodes we talked about how to create a database and how to insert and manipulate data inside a database now in this episode we're going to learn how to actually access our database directly from our websites so as you guys can see in front of me here I have a very basic index of PHP file there's really this is like the bare bones of an index file and what I want to show you guys is how to actually connect to the database and then in the next episodes we're going to talk about how to actually select data and how to insert data and how to manipulate the database directly from our website so the first thing we need to do here is we're going to create a new file because we want to have the connection in a separate file so you can refer to it later on inside our code so inside my new document I'm going to go ahead and create the PHP tags so we have a opening tag now I'll get asked quite often because usually when I have a pure PHP file like this one I don't put in the closing tags and that's because since we only have PHP code inside this file it's just a common habit of programmers to just leave out the closing tag since the only thing this one does is that close off the PHP code and then allow for us to write other code afterwards now since this is a pure PHP file I'm just going to leave this one out so inside the code here I'm going to go ahead and first of all include four parameters we need in order to connect to the database which is going to be the server name the username the password and the database name for our database so the first one here is going to be a variable that says DB server name like so and it's going to be equal to the actual server name of our database now I'm going to save this just so we can get the color that we want inside the code so I'm going to save it inside my root folder inside a folder called includes so I'm just going to go ahead and right-click first create a new folder call this one includes now the reason I'm doing this is because inside my main directory of my root folder I have my index file which is an actual page inside my website now just to make this a little bit neat and this is not something you have to do it's just a common habit that I like to do inside my websites is that I like to create a folder called something like includes or additional files of something that has all the code inside of it that is not seen directly inside the website but has scripts that runs in the background of the website so inside my includes folder I'm going to save this file as DPH which stands for database Handler dot ink dot PHP and now again I have people asking me why do I include dot Inc inside the file name now this is just naming okay this is just for naming purposes this does absolutely nothing technically so if we were to delete the Inc we just do like debase to PHP if you want to does the exact same thing we can also go ahead and write - Inc if you wanted to you can do all sorts of things with the naming I just decided that dot Inc sounds better so I'm going to say DBH studying the PHP going to save it and now we can actually get the colors that we want inside the code so after we have the two double quotes I'm going to write a semicolon and inside the double quotes I'm going to write the name of our server now because right now I'm using a local host server which is using xampp if you're using an online server we need to change the name but because I'm using a local server I need to write localhost so if you guys are following the exact you know same exact program that we used to actually get the server running then you also need to write localhost now the next name is going to be the database username so I'm going to say DB offer so we need to create a variable called DB username and this one if you're using xampp is going to be equal to groups Roo T now this would only be changed if you are an online server because you will have a different user name for your database this would also be teams if you by yourself actually change the username and the password inside your local host database so as a default xampp is going to be root inside the next variable which is going to be variable DB password I'm going to set a password equal to nothing now that it again is a default inside xampp that when you installed as a default is going to have no password if you created a password inside XM when you installed it then you need to change it if you are using an online server you also need to change the password inside the actual Kobo writing here so you need to include the proper password for your online database inside the next line we're going to include the database name so I'm going to say we have a variable called DB name which is equal to double quotes semicolon and inside the double quotes I'm going to write the name of our database now in my case I decided to create a database called login system because in a couple of episodes from now we will be talking about how to create a login system from scratch using PHP code so I'm going to call my database login system and again you can call whatever you want it depends on what you created inside your database inside PHP myadmin okay now that we have the four parameters we need to actually get the database connection running I'm going to include a variable called con which stands for connection it's going to be equal to a PHP function called my sqli underscore connects parenthesis semicolon and inside the parentheses we need four parameters and guess what we just include those four parameters up here so we can just put them inside the parenthesis I'm just going to paste in first of all the server name the username the password and a database name now the reason we decided to write the variables in this order up here is because that's the order that you need to include them inside to connect parentheses down here so we need to have the server name first then a user name then the password and then the database name okay so now that we have this we can actually go ahead create the actual connection to this file inside for example our index file now typically we would actually include the connection inside some kind of PSP scripts that needs to access the database to for example insert a pull out data from the database but just to show you guys how to actually get and you know this access to the database using this file I'm going to use the index file so let's say I have some PHP code inside my body tag down here where we need to select data from the database what I'm going to do is I'm going to include the database connection at the very top so I'm going to say we open up the PHP tags and then I'm going to write include on the score once single quotes semicolon and then I'm just going to go ahead and include the path to this file so I'm going to say it's inside a includes folder because that's the way I decided to do it forward slash and then I'm going to say DB X dot Inc dot JSP so now we have access to the variable up here called variable con which is the connection to a database so if I were to actually connect to the database inside my PHP code in the later episodes you know a couple episodes from now when our server guys have to pull out our insert data directly from the website if you want to access the database we just need to refer to variable con inside our code so that's the way we actually connect to the database and this file here is everything we need to actually do that so we guys injoyed in the next episode we'll talk about how to actually show data from a database directly from our website so if you guys enjoyed and I'll see you guys next time
Info
Channel: Dani Krossing
Views: 721,103
Rating: undefined out of 5
Keywords: how to connect to a database, connect to a database using php, connect to a database in php, connect to a database using php code, how to connect to a database using php code, database connection using php code, database connection with php code, database connection with php, database connection in php, database connection using php, how to access a database, how to get data, how to get data using php, php, connect to database, connect to a database from a website, php tutorial
Id: ILyf16MEvHM
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Thu Jul 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.