Hello today I will be showing you how to create a connection between a MySQL server and PHP. So lets get started!
Firstly we need to define the server settings, The server address, username and password.
CODE
$server = "localhost"; //This is going to be localhost, unless otherwise stated
$db_user = "username"; //Your MySQL username
$db_pass = "password"; //Your MySQL Password.
Next we are going to use the variables we just created to connect to the server:
CODE
$conn = mysql_connect($server,$db_user,$db_pass)
or die ('Error Connecting to Server!');
As you can see it uses the variables to create the connection, although if there is an error when creating the connection; for example the password is mis-spelt, then the user will see the message "Error Connecting to Server!"
Now that we have created the connection we need to select a database. You do this by the following:
CODE
$db_name = "database_name";
mysql_select_db ($db_name)
or die('Error selecting the database!');
Again I have created a variable to assign the database name to, Also you will see that if there is trouble selecting the database it will produce the error message.