How to use XAMPP, phpMyAdmin, and PDO to create and connect to a database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all righty then welcome to this video in this tutorial we're going to you create a database in PHP myadmin i am dave kid from skill forge this video is just going to show you how to create this database and then create a little script in PHP to connect to it the first thing you're going to want is this thing called zamp it is this all-in-one package that gives you everything you need to be able to run databases and PHP and and things of that sort PHP is a server-side language so like javascript in HTML you can just write that on your computer and test it from the browser but PHP doesn't work that way you got to have an environment that supports and can run PHP so zamp is one of the many packages out there I like it a lot just because obviously it's free and it's cross-platform it works for any of these different things that you would ever want it to work for really Mac Windows Linux whatever you're running it'll run so you want to go to their website Apache friends org and then download whatever one you were working with and I'm not really going to show how to install it because it's pretty simple just download it run it keep all the default things and just let it install everything it's not too bad okay so once it's done you're gonna have something like this as amp control panel and you can see that I'm running Apache in MySQL get all these different buttons here you can stop them if you want but we need to have both of these running to be able to do what we want to do here so make sure that you run both of those Apache in MySQL okay once that's running on your computer zamp by default runs off of localhost now one night when you type in localhost the first time it's going to show some welcome screen I got rid of that but if you want to access that folder or everything in localhost is you can come over here to Apache and go over to Explorer not Apache here but at this Explorer button and that's going to bring you into the samp folder and then your local folder where everything you want to run where you want to put that is inside of this htdocs folder so there was an index file in here that i just renamed it intro so now I can kind of get a list of everything that's in my htdocs okay but ne was the main thing here that we care about is just make sure patchy in my SQL running Apache is the webserver MySQL is all the database stuff so we want to create a database so the way we do that is we need to access PHP myadmin which comes with zamp so there's two ways you can do that on the MySQL you can click on admin and that's going to take you out to it or you can just type that in localhost / PHP myadmin first time you log in root is not gonna have a password it'll put root in there don't put in a password and just hit go I've already thrown and created a password so I'm going to do that once you log in you can just say change password and then you can set up a password that way now what we want to do is just create a new database in here now back in the day you had to use SQL commands you had to have some sort of command prompt and go on there and type in the commands and it would do all this behind the scenes but PHP myadmin is basically like Windows like what Windows was for das das was a bunch of commands as well Windows came in with this graphical user interface change the world that we live in right so it's kind of the same thing here PHP myadmin is this graphical user interface for SQL so I'm in here we're not going to go over everything but the main thing I want to do here is click new and I'm going to create a new database and I'll just call it some cool database okay now what you create here you want to remember because later on to be able to connect to this database and do things with it you need to remember what you called it okay so I'm going to hit create okay and then you need to think about how many columns you want in this so the way this works is we have this database here and databases are made of tables and inside of those tables they have rows and columns so now that we've created the database now we're creating a table inside of it and I will call it I guess people and this will be a really simple database I'm gonna get their name and email that's it maybe I want this to be a mailing list database or something so actually we're gonna have three columns and I'll show you why I here in a minute so we got name and email and we're gonna have a third column that I'm going to call ID which is going to be a primary key which we'll talk about here in a few seconds so I got the table called people and I have three columns in there I'll hate go alright so the first column I'm going to call ID and this is going to be auto incrementing and so I come over here I'm going to kind of scroll over to this AI section and I'm gonna check that and when I do that it says oh you want to make this a primary key and I hit go and what I've done here basically is just said okay this I'm not gonna have a lot of control of just at anytime something gets inserted in the database it's automatically going to be assigned an ID and that ID is going to be auto incrementing and so the first one that goes in there will be one and then two and then three and then four and this is this would be what I would use to individually identify people in this database no one will have an ID that's exactly the same and that's why it's that primary key and the auto incrementing will automatically assign an ID to whatever gets inserted in that database okay next thing I'm going to throw in here is a name and that's going to be a varchar' so character basically just means I'm going to put in some characters okay the next one I'm going to throw in is email that's going to be of our char as well and then I just say how long I want these to be just for kicks and giggles I'm gonna throw in a hundred on all of these okay don't think I'll ever have a name that's more than 100 characters same with the email same with the ID but oh well I'm just going to throw it in there so I got the ID got the name I got the email okay I got the type of value it's going to be okay and then I have their links and then I've made that one a primary key the ID so once I do that I'm just going to scroll down here and hit save get down here this is the console here that will usually be down here you can click on the console and this will show you the SQL that it created to do what I just told it to and I come up here and now I can see I have this table with three columns an ID column a name column an email column this one's auto incrementing and you can delete them if they want you can change them if you want again you don't have to know any of the SQL to do this you just go in there and click and drag and do things and it will do what you want okay so now we're going to create a little script that will connect to this now one other thing to be aware of is so you have the root user so if I come on here and I click on this database and then I come over here to privileges it'll show me all the users that have access to this now I created one for Santa because I did another little project a while ago but we have this root so the root user that you have right now should have access to this database so whatever password you gave it when you change the password you're just going to use that okay but if for some reason the root user isn't working you can come in here to privileges click on your database go to privileges and then just add a user account and then what you do is just give it a user name the host you're gonna want to be localhost because your zamp you're running it locally on your machine and then you're gonna just put in a password for that user okay and then you'd come down here I would just give them global privileges just check everything and then come down there and hit go if that's just if for some reason that root user duck can't have access to this database that you created all right so now we go to the samp go back to here explorer and we're going to go inside of that htdocs folder okay and i'm just going to create a new folder here and I will call it not a shortcut I will go new and go folder I'll just call it DB ex4 database example in there I'm gonna create a new file I'll just do text document and I'm gonna call DB PHP now if you're on Windows it's going to yell at you hey that's gonna be kind of stable no it's not I'm just gonna hit yes now you're gonna need some sort of text editor it can be just it can be anything you want the one I use that I recommend is brackets it's really good it's created by Adobe it's free it's cross platform as well there's Adam 8000m that's a really good one notepad plus plus is good for Windows I mean there's a bunch of them out there that's that are pretty good but I'm just gonna go with brackets here so that's gonna be the one I'm gonna use so you can see that brackets is already attaching itself to it all right so that comes up okay I don't want this to be split so I'm going to say no split okay so all I'm gonna do is create a PHP script here now we're gonna use what's called PDO PHP data objects this is an object-oriented way to access databases the good thing about PDO is it is compatible with many different types of databases MySQL all these other different types so if you learn how to do PDO and you use PDO your databases your database scripts are gonna be a lot more compatible with all the different types of databases that are out there okay so in PHP you do variables with the dollar sign okay and I go DSN because this is a data source and we're gonna basically tell it where the database is and which database we're going to mess with so we're gonna say MySQL and the hosts that we're gonna mess with is going to be equal to localhost okay and then we're going to separate that and we're gonna say the database name now remember if you forget you can always go back alright so my database down here some cool database so I'm a cool DB so that's what I'm going to use there okay and I don't want the quote there okay so that's what it's going to use to connect to the database this database object here the PDO okay and we got to set up the variable mine was route but these are strings or characters or words so they need to be in quotes set up another variable and I'm going to put in my password in quotes as well mine is very secure password so watch out for that okay and those are the three things that a PHP data object needs to be able to connect to a database so next thing that we're going to do here is do a try-catch block so we're going to try to connect to the database if we can't we're going to catch the error that happens and then print that error out to the page so what we're going to try is set up a new variable called DB and we're going to set up this new object this new PDO object and we're gonna give it the connection information that we gave it up there on line two we're going to give it the username and we're gonna give it password those are the three things that this object is going to need to be able to try to connect to that database okay and then I'll just throw in here a little echo okay this echo will run if the database connects okay now if it doesn't work we're gonna catch the error and the error stored inside of this PDO exception okay and I'm going to store it inside of this variable called e so PDO exception is just built-in command that just knows okay if any errors happen it's it's called a PDO exception and I'm gonna save it inside of this e variable so I'll set up another variable called error message and then I'm going to use that variable e that would be holding it and I'm going to use this built-in command called get message okay and then I'm gonna exit in other words just get out of the whole script so this notation here what this is doing is it's going inside of this object II which holds the exception and then this is what that means is it's basically going inside of this object that holds the error and then it's using this built-in command called get message this built-in function or method and so we would have to do well actually we need to do this we'd have to echo it out to the page as well so it would go out there get the error message basically store it inside of error message and then we would echo it to the page so if something goes wrong and it doesn't work then it'll give us an error message so it goes out here tries to connect if it does it'll echo we've connected if it doesn't it'll create an error and we go grab that error message inside of e which is that PDO exception stores it inside of error message and then what echoes that out to the page and then it exits the whole script okay so the way we can test this now is we just come over here type in a local host local host will take us inside of htdocs okay so now I want to go inside of my folder DB e^x so DB e^x for database example okay and then I'm gonna run that file that I just created so DB PHP okay there we go you have connected so it looks like it worked now if I come over here and you know accidentally mess up the login okay now I'm starting to get some air so this is the exception to get that got caught and now I'm echoing it to the page so access denied for user Roo at localhost using passwords so that's the type of error that we would get if it wasn't working all right guys so PDO is a good thing to know any questions feel free to always reach out to us or you take one of our courses it's obviously the point of these videos let you guys know and that we're here hopefully to help you with your training needs and help you learn this stuff so you can go back into your job and feel confident about things that you need to do so thanks for watching this yeah hope you guys have an awesome day we'll see you later you
Info
Channel: SkillForge
Views: 71,026
Rating: undefined out of 5
Keywords: php, database, sql, pdo, php data objects, phpmyadmin, xampp, apache, localhost, training, skillforge, online, mysql, mariadb, data, objects, table, columns, varchar, int, dsn, source, name
Id: N676lCQq4n8
Channel Id: undefined
Length: 15min 59sec (959 seconds)
Published: Sat Dec 22 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.