Connect To A Database Using MySQLi and OOP PHP | Object Oriented PHP Tutorial | PHP Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to do another exercise using optical into programming in this episode we're going to learn how to spit out data from a database so in my example here as you guys can see I have a completely empty website except for the basics of an html5 setup and inside my database I have a database called Hopi tutorial and have a table called users now inside the user table I have an ID I have a UID and a password of the user so what I want to do here is I want to spit out this information inside the website so we'd actually see the username and the password so what I'm going to do here is I'm going to go back inside my code editor and I'm going to go ahead and create a new file I'm going to go and save this one inside my root folder inside another folder called includes because I want to give all my class files inside an include folder again it isn't required that you include all your class files inside and includes folder I'm just doing it because it's easier for me so inside my includes folder I'm going to go ahead and save this one as DBH dot Inc dot PHP again that ink is just away from me to know that this is inside the includes folder you don't have to call it the Inc it does nothing it's just for naming purposes okay so call it DB 8 that Inc the PHP inside this file I'm going to go ahead and create a class that has the database connection to my database so I can actually call on this class whenever I need to create a connection so I'm going to go and say if you have the PHP tags and inside the PHP tags we're going to go and create a class I'm going to call this one DB H and open it up now inside the class the first thing we need to do is add some private properties because we want to have the actual values to a database so I'm going to go and say we have a private property called server name and close it off then I'm going to go and include three more because we need to have the database username password and the database name so I'm going to go and say instead of server name in the second one we're going to have user name the third one is going to be password and the last one is going to be DB name now you might be asking why am I making this private well because the database connection is something we don't want the users to get access to we want to make sure that's always private so only methods inside this class here can use these parameters because this class should be the only class that connects to the database okay so after creating these properties I'm going to go ahead and say if you have a protected method called connect now you might be asking why am I making this protected that's because the only time we need to access this is using another class that extends to this class so we won't actually be doing anything other than connecting to the database using this class so we need to make sure that other classes can use this database connection inside this class so when we use protected I can actually extend other classes to this class and use functions inside this class okay so the next thing I'm going to do is I'm going to go and say we have a this the point to server name which is equal to let's actually make sure we spell that correctly sir the name that is equal to a string which will be the actual server name inside my database right now I'm using a localhost so I'm going to say localhost I'm going to copy this paste it down a couple more times like so and change the names so once a username I'm going to say password and database name the user name is going to be root because that's my database username again you need to change these parameters according to what your database connection is I'm going to say my password is empty and the database name in my case is opie tutorial you guys can create some other kind of database name if you want to I just decided to call it Opie tutorial ok so after we did this we need to make sure we actually return the database connection so we need to create a variable called come which is equal to new my sqli parentheses semicolon now the customer inside off to go into programming the MySQL I functions were using are going to look slightly different so in the past it would actually say new MySQL I on the school connect because we're using procedural programming but because you're using object-oriented programming we're going to go ahead and use MySQL I now you might be asking well it's magical like good for optical into programming well there's benefits of using MySQL I and those benefits of using PDO when you want to create database connections and interact with databases using optical into programming and there's pros and cons to both now the reason I'm choosing my sqli is because that's what we've been doing in my other PHP series I have on procedural programming so in case new people jump into this series after the PHP series and want to learn object-oriented programming we're going to go and use the same type of database connection as that lesson series okay so you could also use PDO if you wanted to now inside this - cue live function here this method here because when set off to go into programming we're going to go and includes these parameters so we're going to say we have this server name as the first one the second one is going to be this username the third one is going to be this password and the last one is going to be this DB name okay then after we did this we need to actually return variable con so I'm going to say return variable con like so so this is all we need to do inside the DPH file so now can actually go ahead and include a second class that actually handles getting used information from a database so I'm going to go ahead and create a new file I'm going to go in and copy what we have inside our DBA studying to PHP just to make it easier I'm going to rename the class as user I'm going to save it inside my include folder as user dot Inc dot PHP now I'm going to go ahead and delete the properties in here because we don't actually meet them and I'm going to go and change the method name to get all users so now we did this there's a couple of ways we can do it right now I want to create one class that simply handles all the connections to the database and then afterwards I'm going to go and create a second class called view user that actually shows the use on the website the information based off what we got from the class user and this is because they don't want to show anything using methods inside the browser with methods that also connects to the database I want to separate these to make sure that one file or one class only handles connections and the second class only shows the data based off that connection okay this is one way to do it you don't have to do it this way but this is one of the concepts that goes behind the MVC model we talked about in the beginning of this series here okay so inside this method here or actually inside the class we need to make sure we extend to the database file because right now I can actually get the database connection without extending to the DPH class first so I'm going to say extend and then say we have a DPH file after we did this we can actually go ahead and change the information inside the function or the method here called get all users so I'm going to go and delete everything inside the method and I'm going to go and say well first of all we need to actually get the information from the database so I'm going to say they have an SQL variable which is equal to double quotes and inside the double quotes you want to select all from user let's actually make sure we name that correctly you sir not users and after we did this we can actually go ahead and run the database connection from the DBH class ok so inside the next line we're going to go and say we have a variable called results which is equal to variable this which means the right now we're pointing to something inside this class again remember because we're extending to DBH we now also have access to all the properties and methods in here as long as they're protected or public meaning that we could actually get the database connection here so I'm going to go ahead and copy connect and paste it in right after we point to this class then we need to actually query this inside the database and we use another method building into the PHP object-oriented language called Cori parentheses semi-colon and now we're going to go ahead and include the variable called SQL inside the query here so now basically saying that we want to get this connection and then query the Select statement we have up here okay next line we need to actually check if we had any kind of results so I'm going to go and create a variable called num rose and set it equal to variable result which we have up here and then I'm going to go and use a buildin method called num rows like so which gives us some kind of number based off how many results we got from the database on the next line I'm going to go ahead and say we have an if statement and inside the if statement I want to check if num rows is greater than 0 so if we had any kind of results then I want to get the data then inside the if statement I want to actually loop out all the results and put them inside an array so we can return the array inside this function here and set this method and pass it on to the class that actually needs to show the data so we're going to say we have a while loop parentheses curly brackets and inside the condition we're going to go ahead and say we have a variable called row which is equal to dollar sign results that points to a method build into the PHP object-oriented language called fetch underscore and sock parentheses then after we did this we can actually go ahead and go inside the loop and say we have variable data brackets which means the right now we're saying we have an array called data and we're going to set it equal to dollar sign row meaning that every single result we get from this while loop is going to get inserted into an array called data then after we did this we can go down to the next line and return variable data which means that now we have an array that we can pass on to the next class okay so all we did here was we essentially said that this method here should only connect to the database and get the data then the next class is going to actually handle the data and show to the user okay so now we're going to create a new file I'm just going to go ahead and copy everything with it from the previous file paste it in save it as you user dot Inc dot PHP then I'm going to go and change the name to view user and have it extend to not DBH but user instead because you need to actually get the data from user okay let's actually make sure we have this one protected we do so now that we have this we can actually go ahead and change the method in here called get all users since that I'll get all users I'm going to go and say we have show all users then I'm going to delete everything we have inside the method and I'm going to go ahead and say we have a variable called data which is equal to dollar sign this point to get all users the method we have inside the previous class which is in here this one right here which means the right now data is is going to be equal to the array that got all the information from the database okay so now the next line you can do it for each loop it actually spits out the data from the array so we're going to say we have data that's the variable I'm going to go and delete this right here and I want to spit out a piece of data each time you refer to data not data so I'm going to save that variable data I'm going to go and go inside before you an echo variable data bracket because right now one side an array so we can use brackets to refer to which column inside the array of which T we want to actually spit out so I can say that this one should be the UID again the name for these UID and the next one we're going to do which is called PWD are the column names you have inside the database so the keys inside this array is going to be equal to the column names from the database okay now just to make sure this looks somewhat nice I'm going to go ahead and include a break right afterwards so I'm going to say punctuation string and break and just copy this down to the next one so now that we have this we can go inside our index file and down inside the body tag well first of all we need to make sure we actually include all these files we just created here so inside the index file at the very top I'm going to cuddle them up the PHP tags like so I'm going to say include space single quotes semicolon and inside the single quotes we're going to include the database connection first of course we need to make sure we dig in that includes folder and the reason we want to get the database connection first is because no to actually do anything with the user class or the view user class you need to have the connection first so the connection is going to be the first file we include the next file is going to be the user class because we need to actually have the user class before we can use the view user class okay I'm going to say user the link to PHP and then the last one is going to be the view user dot Inc the PHP inside the body tag I'm going to go ahead and get these data that we got from the view user class so I'm going to go and say we have a object called users what we should actually include the PHP tags first like so let's say we have an optic called users which is equal to new view users like so did we actually call you user with it so let's make sure we don't have an S at the end here then afterwards you can actually go ahead and point to the method inside the view user class so I'm going to go and say we have users and point to a method called believers called show all users parentheses so now if I go inside the browser you guys can see we get oh we get an error message because we forgot to change the actual method inside our view user to public remember the public class allowed for us to actually go to our index file and pull out data so this one has to be public now if you go back to the website you guys can see that we get the users and the passwords again if you want to change this we could go ahead and say that inside the echo inside our view user has a string that says user name is space and then whatever is inside the actual data here then we do another one that says password is and then spit it out so you guys can see we now get some text going so this is how we can spit out data from a database at least one of the ways we can do it hope you guys find this useful and I'll see you guys next time
Info
Channel: Dani Krossing
Views: 136,700
Rating: undefined out of 5
Keywords: connect to database using object oriented php programming, connect to database object oriented php, how to connect to database using oop php, connecting to a database using oop php, oop php how to connect to database, oop php how to connect to a database, oop php connect to database tutorial, connect to a database using object oriented php programming, mmtuts, object oriented php, php tutorial, connect to a database, mysqli, connect to database using mysqli, mysql database connect
Id: PHiu0JA9eqE
Channel Id: undefined
Length: 16min 33sec (993 seconds)
Published: Tue Mar 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.