Connecting to MySQL with PHP
Before you can create, modify or search a MySQL database you must first establish a connection to the MySQL server. The PHP function mysql_connect() will do just that, connect to MySQL.
The connection to MySQL will automatically close once the script which called it has finished executing. You can close the connection before the script ends by using the PHP function mysql_close(), which is recommended.
PHP's mysql_connect function will accept up to five parameters.
* Server
* Username
* Password
* new_link
* client_flags
Typically, you will only use the first three parameters. So let's get on to the PHP code to connect to MySQL.
<?php
// Connect to your <B style="color:black;background-color:#A0FFFF">MySQl</B> server
$server = "62.69.64.179";
$user = "your <B style="color:black;background-color:#A0FFFF">mysql</B> username";
$pass = "your password";
$conn = mysql_connect ($server,$user,$pass) or die(mysql_error());
print ("You connected to <B style="color:black;background-color:#A0FFFF">MySQL</B>!<br />");
mysql_select_db ("name of your database") or die(mysql_error());
print ("You selected your database!");
?>
1. Copy the above code and paste it into your text editor such as wordpad or notepad
2. Edit the username and password variables
3. Save the file as "connect.php"
4. Upload the file to your server
5. Load the file in your browser
Important: We recommend that you create a folder on your server named "/includes/" and store this PHP MySQL connection script there. Then, any time you need to connect to MySQL from a PHP script, you may include() your connection script like this:
Include($_SERVER["DOCUMENT_ROOT"]."/includes/connect.php");
Connect.php, Explained
$server = "62.69.64.179";
$user = "your mysql username";
$pass = "your password";
The first three lines just put your MySQL connection parameters into variables. Alternatively, you could just put the parameters directly into the mysql_connect() statement, it just looks neater this way.
Note: If you get an error stating that your username or password are incorrect, check with your host to get the proper connection string.
Moving on...the actual MySQL connection.
$conn = mysql_connect($server,$user,$pass) or die(mysql_error());
Here is where the connection to the MySQL server is made. If the connection is not made successfully, the "die()" section will be executed, giving you the reason why it failed to connect to your MySQL server.
Upon successful connection, a unique link id is stored in the $connection variable. You may use this variable in later in your scripts to refer to the specific MySql link. This will become useful if your script needs to connect to different databases.
print(" You connected to MySQL!");
mysql_select_db ("name of your database");
print ("You selected your database!");
?>
A simple print() statement to verify a successful connection, followed by the mysql_select_db() function and another print() to verify that the db was selected. You have to select a database before you can query it.
Remember, save the connection script here:
/includes/connect.php
...and store it on your server. This universal script will be used anytime you want to connect to MySQL.
You may run this this script now by loading it into your browser. (eg. http://www.yoursite.com/includes/connect.php)
