Query A Database Using PDO In OOP PHP | Object Oriented PHP Tutorial | PHP Tutorial | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last episode we learn how to connect to the database using PDO and in this lesson you're gonna learn how to query the database using PDO so what we did in the previous episode was we created a basic DP 8 class which had a database connection inside of it using PDO and what we're going to do today is I'm going to create a second class called user because inside my database I have a users table that has two users inside of it and what I want to do here is I just want to select the data from inside the database and spit it out inside our website now inserting and updating database works the exact same way as selecting so we're not going to get into all the different different ways we can query the database you should know that by now so what I'm gonna do here is I'm going to go inside my index page and I'm going to include a second document so I'm just gonna copy paste and the second document which is going to be the bottom link is going to be called user dot Inc the PHP now according to the MVC model which we talked about in the previous episodes we have a user that Inc the PSP which is a class that handles all the connections to the database and then we have a second class called view user that takes care of actually showing the data to the user inside the website now just for the sake of this episode I'm just going to do both of them inside the user class so don't get confused by the fact that I'm getting data inside the user class and also showing the data inside the user class ok so I'm gonna go and save this and then I'm going to create a new object of the user class which we haven't created yet but we well in just a second so I'm going to say we have an object that is equal to new user and then we're gonna go ahead and run another method want to do is to create the user class called get all users again we haven't created these yet but we will in just a second so what I'm going to do is I'm just going to go ahead and create a new document I'm gonna go and save it inside my include folder I'm gonna save it as user dot Inc the PHP then we want to open up the PHP tags so can actually write PHP code in here and then I want to create the user class so I'm going to say class called user that extends to our DPH class because we need to actually connect to the database so we need to extend to the database class that has the connection in it DB H with a Big D curly brackets and then inside the class if we can go and add the methods that we need to use in order to get the users and actually show it inside the database so what I'm going to do is I'm going to create a public function or public method called get all users which is the one we refer to inside the index page so now what we need to do is you need to actually connect to the database and get the data that we want to get which in this case is going to be the information under users inside my database now there's two ways we can do this the first way is if we don't have any kind of variables that we need to insert inside our query meaning that if you want to select all from users where use ID is equal to something that the user provided inside the website then we need to do it in a different way than if we were to have no variables inside the connection now I don't have to but it's much faster to do it this first way I'm going to show you if you don't have any variables okay so the first way is by creating a variable called as TMT which stands for statement which is going to be equal to and then I'm going to point to this connect method which you have inside our database connection if you go back inside the database class you can see we have the method right here where we connect to our database using PDO so I'm going to point to that specific method and then I want to say we have a query method like so semicolon and inside the query we're going to insert the the statement that we want to EXCI query inside the database so in this case here I'm going to say we have a select statement I want to select all from users which is my user inside the database then underneath here we can just simply create a while loop and inside the parameter of the while loop I'm just simply going to create dollar sign row which is going to be set equal to the data we get from the Corre inside the database so going to say variable is TMT then we want to fetch or run the fetch method and this is basically all we need to do so inside the while loop I can just simply echo or return some kind of data so if we were to say something like variable UID which is the username it's going to be equal to dollar sign row brackets then inside the brackets we can say we want to get the UID column data so which in this case is going to be John I'm pretty sure then underneath it we want to actually return the variable UID so we can actually get it when we do actually call on the method like so and then if I were to go back inside my index page we need to make sure we do actually echo out the method called get all users I'm going to say echo and then if we were to go inside my website refresh you can see we get John if what axis zoom in so you can see it so this is how we can get data from inside a database using PDO now if you do actually want to run a query that does require parameters which in most cases if you were to create something like a login system you will need to have inside your query then we're going to do it slightly different because we need to make sure we include prepared statements when we connect to the database otherwise our connection is not going to be secure so what I'm gonna do here is I'm gonna go back inside my code and I'm going to go back inside my user class then I'm going to create a second method because I just want to create a new one with another example I'm gonna call this on public function and I'm gonna call this one something really long something like get users with count check because what I also want to show you guys in this example here is how can actually get how many results from inside the database now there's a couple of ways we can get the amount of rows that we did actually get using our quarry the most efficient way would actually be to do it directly inside our course statement you know inside deselect all from users where blah blah blah but in this example I'm going to show you guys how to do this using PHP code so the first thing I'm going to do inside this method here is I'm going to create the two variables which are going to have the information that the user would actually have submitted to this method in order to get some kind of data from the database such as locking in I'm doing this because it would be much too complicated to create an entire login system to test this out so we're just going to create two variables here called first of all ID which is going to be equal to 1 which is the first use of inside my database and then we're going to create a UID variable which is going to be equal to John because that is the name of the first user inside the database or his username underneath here we're going to actually create the actual query so going to say we have a variable called statement or is TM T which is equal to variable dis which points to a method that is either inside this class or inside a class that we extend to so we're going to run the connect method from inside the DBH class so going to point to connects parentheses and then afterwards we're not going to point to a Cora method instead we're going to run a prepare method that we have inside object-oriented programming and the prepare method is going to have the actual query inside of it so going to say select all from users where ID is equal to question mark and UID is equal to question mark and of course do you need to wrap this inside quotes now the question marks right after the equal signs are going to placeholders because we're using prepared statements again if you guys don't know about prepared statements I do have a tutorial that shows how to use prepared statements in my regular PHP course okay so you can go back and watch that one if you're a little bit confused about prepared statements then underneath iya I'm going to take the data that we have up here called one and John and insert them instead of the placeholders inside the query here and again you should recognize this if you're familiar with prepare statements so I'm going to say we have our statements that we have up here and I'm going to run a execute method and inside the parentheses I'm going to say brackets and then insert the data that we have up here so I'm going to say we have an ID comma and then a UID and paste it in and what you're going to notice is that prepared statements inside PDO is a lot easier than using it with MySQL I which is a good thing because we don't want to write too much code when we create applications now the next thing I want to show us how we can actually get the number of returned rows from inside a quarry so what I'm going to do underneath here is I'm going to create an if statement and inside the if condition I'm going to say we have our statements and point to a method called row count parentheses which is going to count how many rows who returned from inside the query now we don't actually need to check if it's greater than or equal to anything because if you don't have any rows then this is not going to return anything which means the right now we're actually doing a boolean which is a true or false statement so if we had any kind of result then it's gonna go and run the code from inside the curly brackets else it's gonna do something else okay now I'm just gonna go ahead and leave out the else and instead we're gonna go ahead and create our while loop just like we did on the above method we created before so we're gonna say wow something is true curly brackets then return dollar sign row brackets and then we want to return the UID of the user and then inside the condition of the while loop we're going to say we have dollar sign Rho which is equal to the statement and point to fetch like so if I were to go back inside my index page and change from get all users to get users with count check then I can actually go inside my website refresh ah so did actually get an error message and it seems that I did not spell execute correctly so we need to change that so we'll go back inside the code inside users and then go ahead and say execute like so and now we have John and this is basically how we can get all the users from the database using prepared statements together PDO as well as take how many rows of data we got from the query now that is one thing that I accidentally skipped past which is something that some of you may ask me about which is the fact that inside the first method we created that simply got all the users from the database I only had John output inside my browser and I do actually inside my database have both a user called John and another one called Jane and we didn't get both of them output inside the browser now the reason for that is that inside might get all users method I returned the user from inside the while loop instead I would actually need to go in the lead to return and instead of saying Urd equal to I would echo then replace the method from inside the next page and then you can see that we get both users so the practical approach it would actually be to return an array of data instead of echoing out the users inside the while loop okay so if you guys enjoyed I'll see you guys next time hey guys hope you enjoyed this video I just want to say thank you for the support you've given me on YouTube and gave out a huge thanks to the people on patreon who supported me on patreon on a monthly basis and if you're new and you don't know about patreon you can go ahead and click on the link in the description of this video and it'll take you to my page where you can pay a small amount every month either to support me or to download the lesson material for my lesson see on the so hope you guys will take the time to go and check that out and I hope to see you guys next time
Info
Channel: Dani Krossing
Views: 55,268
Rating: undefined out of 5
Keywords: How To query A Database Using PDO PHP, query a database using pdo php, query a database using object oriented php, query a database, query database, oop php, object oriented php, select from database oop php, select from database using oop php, query database using oop php, get database data using oop php, query database using pdo php, how to query a database using pdo php, how to get database data using pdo php, pdo get database data, pdo query database, php, php tutorial
Id: m_-vmSv2PFo
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Mon Jan 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.