Tutorial: Connect to MySQL Database
In this tutorial we're going to connect to a MySQL database with PHP. You need this knowledge if you want to access MySQL databases and insert items, delete items or display items from these databases. For this tutorial you need a MySQL database with accessing-data which you will find at the control-center of your web-provider.
First you need a host (examples: host.test.com or localhost). Then you need a username, a password and a database-name.
It is recommendable to save these data in some PHP variables, because they can be easier to remember.
Once you saved your data in some PHP variables you can connect to a MySQL database.
This is the code to connect to your database with help of the PHP variables.
Note that you also can use no variables and add your data between the quotation marks. Also note that you
have to use the order of variables we use in mysql_connect
.
You only need the first three variables for the mysql_connect
function, because it only connects to the
host.
mysql_select_db
selects the database. The rule or die
can show a message if there's no connection.