Connect to MySQL with PHP in XAMPP / Create a new database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome in this video I'm going to cover how to connect to my sequel database using PHP and so you can see i'm using xampp here i'm just going to work off the control panel so if you have xampp you should be probably doing it like this i'm going to click on the admin button next to my sequel what this is going to do is going to kick me over to PHP my admin now if you are working with a live host you probably have this tool available to you or something similar it's just your job to find it when you have a free host or something you might not but if you're paying frosting you probably have this tool and just your job to find it so really I said we're going to connect to a database we need to we need to create a database first I mean I've got some over here but let's create one so I'm gonna click on the databases tab and we'll call it test DB write basic as it gets I'm just going to go with the default collation which is basically how the physical data is represented on disk so I create that and now I've got a database called test dB now I can connect to this database now if I was really doing something I would set up users and passwords and everything like that but I really want to talk about xampp for a second here X am to say that security is an afterthought is not really accurate xampp is disabled every security which it can so when you connect to a database you could have a username and a password the default username and password for the super user is root and no password so that's relevant because I'm going to go here and I'm going to create a connect script so here I am in notepad plus plus I'm going to write a little PHP here just opening and closing tags nothing yet so so really there's three variables which we're going to be concerned about the first one is a user now as I said in xampp it's just root that's the that's the username you can create more if you have a live website it's not going to be root right there would be a big mistake if you were really had a user name of root the other thing is going to be the password and that is literally just blank and so in xampp they've disabled everything they can disable just to get you up and running and testing your site you can adjust these things but by default they're not there and the last variable is just the name of the database mine was testdb you're seeing a little PHP syntax here it's not the friendliest stuff in the world but that's what it looks like and really so the magic part here is just one line and so this connection is very well going to be used multiple times so I'm going to store it in a variable such as this and really what I want to do is I want to create a new MySQL I and then here's a function and I'm going to call that function so that function takes four arguments the way I'm going to use it the first one is localhost now this is going to work on xampp this would likely work on a web server as well it might seem like it's it would only work on my computer because I'm running xampp but that's not really the case what that really means is that the my sequel server is on the same servers as Apache and it is in some cases if it's not you might have to do some kind of an absolute link right there the next piece of information is the username and the last is the password sorry and the database and so you can see why we used variables here because if you have a real website out there just substitute whatever username is substitute whatever your password is if you don't know what that is check your email as typically like as soon as you pay for something you get an email with all these you know usernames and passwords and it's probably in there or get a hold of support on that in theory should work but it's good practice to write or and we're going to call a function called die all right so what die means is if this piece right here if the left hand of this expression doesn't go well some reason I have a syntax error then it's going to die and die just terminates the program right there and it spits out a message like unable to connect it'll tell it'll spit out whatever eyes hell it's spit out and so it's either going to be successful or it's going to die and this at this point I think it's time to save my file I'm going to call it connect dot PHP notice the file extension it's going to yell at me cuz I already have one I just tried to make this video a minute ago but the audio didn't work I think I replaced it and let's see if it works so I'm going to run it nothing happens nothing should happen because I need to run it through the localhost write PHP is not going to be interpreted if I don't now I run through localhost and nothing happens that's a little concerning so recall what this does is either creates a connection or it fails there's no output so it's good practice to just do a little echo down here and my echo is going to say like a great work right little enthusiasm layer and so that might look like it would happen every time but in theory it's either going to successfully create the connection or die right and if in in the case that it dies it's never going to get to this line because it just stops right there so I'll save this and you'll see why I did in a minute and so I refresh and great work read the implication that it didn't die and I didn't get any warnings so it probably worked so let's let's see what it looks like let's say the user did not know a password alright save that i refresh and she not so great work I still get the great work because it didn't technically die I just write access denied so it's a warning warnings don't warnings aren't a complete failure warnings or just are just bad and you should address them if it's kind of sticking out to you you're thinking my gosh that seems kind of weird then the password is just in plain text well it is and there's all kinds of things you can do to make that a less difficult situation or right I mean you can do a better job than what we're doing here but in theory all you got to do is substitute in the values that you want and so at this point you should be able to connect your database so this this is how it is by default in xampp if you want to create users or you know you've got a live website with some real user names and passwords all you have to do is substitute that's the kind of beauty of writing it out like this you should be up and running at this point from here you can actually do some database things like query your database and just connecting is nothing but really this is something that you could get hung up on so now at this point you can connect and and from there you can create some tables and and do some meaningful things thanks for watching
Info
Channel: Ken Swartwout
Views: 1,499,760
Rating: 4.729991 out of 5
Keywords: Tutorial, Programming, PHP, MySQL, Connect, Mysqli, User, Password, Localhost, XAMPP, Create Database, Die, Function, Error, User Name, Connection, Variable, Table, PHPMyAdmin
Id: ueWpNe0PG34
Channel Id: undefined
Length: 6min 40sec (400 seconds)
Published: Wed Dec 11 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.