CRUD with PDO and OOP PHP #2 - Display Data From Database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back all right this is what we have from the last video and basically we create this post title and body and the author as well from the heart code in here okay you don't get any data from database or something like that so in this video we are gonna read database from our P block database and we have a table called pools and we are gonna showing this data which is titled body and utter to this page all right so let's let's create another folder another file I mean inside a class we are gonna create a file called DB ha dot class dot PHP alright so inside in here let's open PHP tag and just remove the ending PHP tag because this is just full PHP or gonna typing in this file so let's create a class called DB ha and and in here let's set a public and let's create a property called yes name just for example and let's just say hello variable and we put hello world I cannot type in hello world oh man I'm so real stupid okay save and now so let's go to includes folder and create another file in here let's create a class dot this autoload dot include dot PHP all right in this class autoload let's create our PHP tags ring of that and we need to call function it's SP l dot how to load dot register okay and so let's create a function in here function call out load we're gonna facing name that's cos name cross name and in here we need to do is create a path variable and inside in here let's create classes just this folder classes and class for the extension extension it just acts something like this so make that sort soul in here let's say dot class dot PHP and let's create a file name it's going to be equals to don't need the quote let's do in here path concatenate with the class name and the extension all right so that's just for checking in here if file not access file name we need to return false and in here let's say include punch file name alright so in index dot PHP make it a little bigger we need to include in clueless folder and we're looking for classed as autoload dot in cut that PHP okay it's gonna be looking for this file and oh yeah we need to add this function to this auto load register we need to using double quote in here so let's test the syntax okay so safe and what happened in basically is I'm gonna show you right here okay so as you can see in here we are gonna include this file name variable the final variable we get a path all right this is the classes which is this folder and class name right here it's gonna be represent everything what we are gonna instantiate object in here okay we are gonna doing now okay just for example let's create just say hello I'm gonna say equals noon and DP ha all right so just by doing this instance here TP ha it is right here and this class basically what happened is this class name is gonna be changing to be DB ha because we have instantiate that right here okay so what happened here is that it's gonna be classes slash filename it should be DB ha and extension is going to be dot class dot PSP it's gonna be looking for everything we are gonna instantiating here if if you instantiate another class in here maybe person it's gonna be looking for classes last person dot class dot PHP okay now that makes sense so now in here what we need to do is it will echo say me say hello and you're gonna looking for the say hello right here okay so now safe let's see what we have okay we have we have a hell of words so it's it's it's work okay so that's cause the auto load file and in here all right so we don't need this so let's get rid of this say pre fat and the whole house is gonna be equals to localhost second is user of course it's depending on what user use use for the default you can use a road in here and just copy and paste that and it's gonna be the password and by default is empty send data and see database name it's just gonna be opie block gets removed all right so I'm changing my username I said John and my password is at me okay so in here let's create a public function and we call that connect to just for make sure it's work I want to is just return this dot hosts not dot host okay so basically we are gonna return the local host when we run this method okay so let's try the things too [Music] holes and have a fax function call connect all right all right so let's say that and let's see what they have all right so everything is work just fine so let's go back to the DB high class HP and we don't need this okay what we need to do is create another variable call PSN's gonna be equals to my sequel horse it's gonna be equal to I'm gonna call this hole so we need to you using this keyword and the house alright and let's create another string in here DB name it should be equals to you're gonna call this DB name all right so under here let's create another variable called PT o and we are going to instantiate new PDU in here we are gonna send DSN variable in here the user and a password so this user coma and this password alright so in here when it's two is seeing video and we are gonna call function calls a tree would set attribute as mature spell right set attribute cool and in here I need to do a PD all or no create a default that's mode using capital word same default fetch mode come on and fetch basically you can use fetch object in here if you want say return you know return the PDO all right so save and let's create another class call post dot class dot PHP okay in here we need to open PHP tag remove the ending tag and let's create a class call pose and it's going to be X tends to Divya all right let's create a public function say get pose get pose and what we need to do in here is create sequel steadman SQL statement and we organized sound like everything from the table name it should be post alright when it's between here is pretty variables and it's going to be equals to this you're gonna call connect function this is from right here okay and we can call this function because we create this class and extents in DB huh right so this connect and we need to prepare that because using video prepare sequel in here and finally let's execute and we don't place anything in here because we just want to grab everything in post we don't send any you know what's fail you or something like that all right so in here I need to do is just just checking while result is equals to Stedman there's gonna be fetch all and if while is true we need to return the result [Music] okay so let's instantiate this class is make sure everything is work so in here we need to do is just remove everything in here let's create post variable is gonna be equal to new pose okay and just for make sure that spring are the posts right here okay so now say if let's see in the browser what we have and we refresh all right so finally we get local host and all right just by print are the post meaning you print this every object in here we don't need property we need to access this get post so let's go ahead and do that so right here to do is get post function method all right so let's reload that out and the fern variable is TMT okay post class what we have wrong in here okay it's it's should be have a tea in there okay so real up again so now I hope that work it's not showing anything in here because it just returned that it's not showing anything in the browser because we didn't use any a whole or print arrows on the other so let's cut that out so bring our bring underscore our and paste in here okay I hope it's showing something from database which is to Dida so reload and now we get the data okay cool now we have the ID 1 title post one body this is the post one after we have which one do I so well okay it's it's it's work so we don't need put in here so we need to put under in here alright so in here basically let's create a row and inside in here let's open a PHP tag and let's instantiate that again pose it's gonna be equals two new holes like that and in here what we need to do is just checking if we have a data or if we don't have any data in your data okay so if only to open gets been here so be if hos you know call the function called get pose right get pose if this true we need to run everything inside in here just make sure we close the if Stedman in here so let's create PHP and we're gonna using and if right here okay so neatly using else statement so we need to PHP and alls right here okay so if we have data what we need to do is just each one show data and else and it's just part off in here boss is okay so now say if let's see in the browser so now we have short data because we have data all right so let's close that out let's remove that now let's create for each in here so PHP for each I need to say pose to call the get pose in here say yes we need to call that pose for every single post just make sure we and for each so and for each alright so what we need to do in here is just copy everything in here okay just copy that copy and we are gonna paste inside and for it's right here and make sure everything is good pe look all right so this is yeah it's really cool so for a title we need to grab PHP you can type in a who in here or just equal for a percent hey-ho you need to grab the title right so pose title and this is for the body right for the content so let's remove the hard code right here and this open PHP and a hole to pose and it's it's gonna be the body and outer as well you need to open a PHP tag in here and it should be a hole I'm doing that it's gonna be a whole pose and should be out there all right so now say if let's see I just want to remove everything hard code in here 3 with that you don't need that so now save and just take a look right here what we have here we go we have a 1 pose from database all right so that was really cool alright so for the next video we are gonna make this work so we can add post and the post it's gonna be automatically go to this index page all right so just make sure you stay in this playlist so I'm gonna show you in the next video and bye
Info
Channel: Cand Dev
Views: 3,672
Rating: undefined out of 5
Keywords:
Id: -oJxVAq6hCI
Channel Id: undefined
Length: 23min 14sec (1394 seconds)
Published: Mon Jan 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.