PHP how to connect to MySQL database

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back everybody we have finally made it to the portion of the series where we will be connecting to a myosql database at this point in the series you will need to be familiar with mySQL queries I do have a full course on that on my Channel for free it's about three hours but yeah moving forward we will need to know MySQL there's two popular ways to connect to a mySQL database the first being the mysqli extension this is what we'll be using the other is PDO meaning PHP data objects many developers prefer PDO over mysqli because it can connect to more than just a mySQL database I believe you can connect up to 12 additional databases you know postgres being one of them however you would need to know object oriented programming which is an intermediate topic and we have not covered that yet as beginners we'll stick with the mysqli extension it's procedural alright well let's get started we'll need to create a mySQL database within our zamp server you'll need to open up the zamp control panel which is this thing make sure that these two modules Apache and MySQL are both started we will need to access phpmyadmin which you can do so by clicking on the admin button next to mySQL that should bring you to phpmyadmin otherwise you can just type in this web address localhost slash phpmyadmin phpmyadmin allows you to configure your database you can make SQL queries monitor the status export import data we'll be covering just some of the basics to create a database go to the databases tab we will create a database come up with a database name in the MySQL series we created a database named businessdb we'll stick with that but you can really name it anything then we will hit this create button we can create a table but we'll do that in a future topic let's be sure that that database is actually created let's click on our server go to databases yeah it's right here to drop a database you can check the database that you create then press the drop button but we don't want to do that but that's how there is some information we'll need about our MySQL server if you go to user accounts we will need some of this information such as the host name the username of root if there is a password for the server which there currently isn't and that's about it you can edit privileges too but that's outside of the scope of this topic all right we have now created our database so let's close out of phpmyadmin make sure that your MySQL server is running it currently is I'm going to create a separate PHP file just to manage our database connection so let's create a new file I will name this database.php anything related to connecting to our database we will handle within this PHP file this will be a PHP script we will declare a few variables the first will be DB underscore server this holds the name of the server for us that was localhost then DB user that was root a password DB underscore password I'll say just pass we did not have a password I will leave that MD then the name of the database DB underscore name I named my database business DB then we will declare a connection variable we'll shorten this to con meaning connection I will set that to be empty okay these are the variables that we'll need to establish a connection to the mySQL database we will take our connection variable set this equal to the MySQL I underscore connect function there are four arguments within this function the database server name username password and the name of the database let's add these variables as arguments so server user then password then database name let me make some more room I'll put these on a new line just for readability but there should be no change to its functionality if I do this if we establish a successful connection this variable is technically what is known as an object we haven't discussed object oriented programming it will represent our current connection one of the few ways in which we can check to see if our connection is up and running is we can use an if statement then place your connection within the if statement if a connection exists let's Echo you are connected else for testing purposes let's Echo could not connect I will save and reload everything then go to your database PHP file localhost slash website slash database dot PHP you are connected all right I'm going to stop the MySQL server then try and reconnect we get this ugly error message fatal error uncut mysqli SQL exception for some reason if we can't connect to our database we don't want to display this error to the user we should use some exception handling we don't want to print any ugly error messages to the user they will have no idea what's going on I suggest when we attempt to create a connection we surround this code with the try block this has to do with the topic of exception handling we can try some code that might cause an error such as if we can't connect to our database let me just fix these we will try and make a connection if we encounter this exception I'll copy it we can take some other course of action after our try block let's add catch parentheses curly braces then add the name of that exception within the set of parentheses in place of displaying this error to the user let's Echo a message such as could not connect and I'll steal that here I'll get rid of this else statement so if we encounter this error again we will display could not connect that's a lot more obvious as to what's going on instead of that fatal error message if I were to start the MySQL server again then reload we are now connected this PHP file is now complete make sure to save everything we're going to close it I will head back to our index file let's generate some HTML after our PHP script in the body let's say hello doesn't really matter what you say let me zoom in a little bit since everything related to our database connection is handled within a separate PHP file we can include that within another file within a PHP script at the top of my index page I will use the include function we will include that file database.php to connect to our database so let's see if it works you are connected hello maybe I'll add a new line after could not connect I'll add a line break do that here as well much better technically it's not necessary to tell the user that they're connected to the database we were just more or less doing this for testing purposes one way or another though we should let the user know if there's any problems with the connection so we'll keep this for now if I were to stop the server reload we have that message that says could not connect all right everybody so that's how to connect to a MySQL server in PHP
Info
Channel: Bro Code
Views: 7,550
Rating: undefined out of 5
Keywords: PHP course, PHP tutorial, PHP MySQL, PHP coding
Id: -1DTYAQ25bY
Channel Id: undefined
Length: 8min 49sec (529 seconds)
Published: Mon May 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.