Connect To Database Using PDO In OOP PHP | Object Oriented PHP Tutorial | PHP Tutorial | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I gonna learn how to connect to database using PDO instead of using MySQL I which is active I've been using up tuned on these lessons here so from today on in the future we will be using a PDA connection in order to do our course here so as you can see in front of me here I have a basic index page and inside the index page I have nothing special except for a link to my class that has my database connection inside of it and inside my DPH class I still have a masculine connection going because I want to show you guys the differences between MySQL I and PDO by just simply using the same class that we've been using in all the previous lessons here now the first thing I'm going to do inside the db8 class is I'm going to add a fifth property inside the class and this is not something we have to do we could actually do PDO connections without this fifth parameter but it allowed for us to create some additional functionality later on if you wish to so I'm just gonna go and include it since it doesn't hurt anything so I'm gonna go and include another private property that I'm gonna call chars set which is the character set the views inside the database and inside the connect method here I'm going to just copy paste from the previous one and just change it to char set and change the value to utf-8 MB for which is just a default character set so not only have this we can actually go ahead and change this MySQL I connection that I have down here into a PDO connection do notice that we have one two three four parameters in SAP mask uli and when it comes to PDO we only have three now the difference here is that inside the first parameter we're going to include a bunch of parameters if that makes sense so let me go answer guys what I mean I'm gonna go and delete it here and what I'm going to do is I'm going to create a variable I'm gonna call DSN which stands for data source name which is going to be the first parameter so we're going to include inside our PDO connection so I'm going to set it equal to a string and inside the string we need to add a couple of different pieces of information now the default ones that everyone adds is first of all we need to tell what kind of driver we're using then we need to add what host we're using and then the database name now the last one that we're going to add is also the character set which we included as our fifth parameter now the most typical database driver that most people are using is called MySQL so if you're using PHP myadmin inside your localhost then you're probably using mask ul as well now if you're not using localhost of PHP myadmin or that sort of thing and you're a little bit confused about what your driver is there is a way we can actually get the information from our database and know what kind of drive we'll be using so for were to go inside my index page and just simply write a print our function print underscore our parentheses curly brackets and inside the print R I'm going to say we want to run a static method in PDO called PDO colon colon gets available drivers parentheses and if we were to go back inside my website you guys can see that we get some information regarding the drivers inside our database now mine does actually say MySQL so if yours does as well then you just need to follow exactly what I'm doing in this episode if it says something else with you then I recommend googling what you need to change depending on what kind of driver you have okay so I'm gonna go back inside my code I'm just gonna go ahead and delete the printer function and I'm gonna go inside my database connection again so the first property inside mat the Ascend variable is going to be my SQL colon and then we need to add what kind of host we're using so I'm gonna say host I'm gonna set it equal to and then it needs to be equal to our host which right now would be the server name which we set up here now I'm using a local host if you're connected to an online server then you need to get the server name for that server so because I'm using local host and we just simply go inside my code here and then after the equal sign we just simply add in the server names I can say you want to add this server name and then I want to add another string because we need to add another parameter to it or another property so I'm going to say semicolon and then we're going to add the DB name which is the database name I'm going to set it equal to do the same thing here and to simply refer to what we have up here so we're going to say we have DB name which is equal to my case test DB because that's what I named my database name so I'm just gonna paste it in here and then the last parameter we're going to add is going to be the char set so we're going to say char is set it's going to be equal to and then we're just simply going to add the charset from up here as well like so so now we have everything we need to have inside the DSM so what I'm going to do now so I'm going to go down to next line and I'm going to create a variable called PDO which is going to be the actual PDO connection so I'm going to say we have a new PDO in capitalized letters parentheses and then inside the parentheses we just need to add three different parameters the first one is going to be the dsm images created from up here so I'm going to paste that in comma then the second one is going to be the username for a database which we have up here just gonna paste that in the third one and the last one is going to be the password so I'm going to just copy paste that one in as well like so now the last thing we need to do so we can actually connect to the database using other classes is we need to actually return this PDO variable so I'm going to say want to return variable PDO like so now there's one more thing I want to do here because what if we get an error message while connecting to the database well what we can do is we can actually go ahead and set up a try catch exception which means that we can try to run a piece of code and if it gives an error message then it's going to catch the error and throw an exception so what I can do is I can go ahead and say we want to create a try-catch function and again I used to shortcut inside my text editor by writing try and then tap and then I got to try catch exception if you don't have shortcuts inside your text editor you can just go ahead and write the whole thing out now what I want to do here is I would like to include our code that we just wrote copy it delete it and then insert it inside our tribe function up here after we did that we then need to set up some attributes inside the PDO connection in order to get a proper error message inside the caps function so what I'm going to do is right before I return the PDO variable I'm going to go ahead and say we want to take the PDO variable and run a set attribute method and then I'm going to go and add a couple of different parameters inside the method here the first one is going to be PDO colon colon which again is a static then I'm going to set this one to attr which stands for attribute underscore er our mo de which stands for error mode then I want to set the arrow mode afterwards by saying PDO colon colon which is a second static and I want to set errm au de which is error mode underscore exception like so so what this does now is if we get some kind of error message then inside the cat's function it's going to take the error and we can then show it inside the website so what I can do is I can actually go and make a couple of changes to the default code I got from my shortcut and instead I'm going to say want to get a PDO exception which we're going to set to variable e which stands for exception then inside the catch function I'm going to echo the error message by saying connection failed colon space then I can go to add the exception afterwards so we're going to include the variable E which is the error message then we're going to point to a method called get message which will now get the actual error message if there is actually an error so for words I actually tried this and go inside my index page and simply create an object and try to actually run I'll connect a method then I can say we want to create an object I want to set it equal to new DBH then underneath iya we're going to actually run the connect method so we're going to say we have variable objects and point two connects parentheses semi-colon so now if I go inside my browser refresh you can see we get some kind of error message and that's because right now my method is actually protected so we need to change it because this is not the error message we want to see so I'm going to change it from protected to public so we can actually run it inside the browser and now if we were to actually run it you can see we get no error messages and that's because everything is like it should be if it were to actually change some of the properties inside my connection to let's say test DB - which is not a database that I have save it refresh the browser you can see we get a connection failed and then the error message so right now it says unknown database test DB 2 which is what we want to get so if I were to go back inside my code and go ahead and just set it back to test a B so everything is actually working so this is how we can set up a PDF connection and in the next episode we're going to talk about how we can actually spit out data from the database using PDO 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 less material for my lessons here on the channel 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: 96,534
Rating: undefined out of 5
Keywords: connect to a database using pdo php, how to connect to a database using pdo php, connect to a database using pdo, mmtuts, database using pdo, PDO in OOP PHP, pdo oop php tutorial, pdo php mysql, pdo php example, mysql pdo, php pdo tutorial, php pdo, pdo, pdo in php, oop php, oop php tutorial, Learn OOP PHP, pdo php, programming tutorial, tutorial, php, learn php programming, learn php, php tutorial, connect to a database, pdo php tutorial, Learn OOP PHP backend programming
Id: yWJFbPT3TC0
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Tue Jan 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.