Using PHP to connect to the MySQL server and database.
A sample connection script »»
Copy everything into notepad from <?php to ?> (including the tags) ...
Save the file as connection.php or similar (in notepad you'll need to choose "All Files" under "Save as type").
<?php
/******************************************************************************* A PHP script to set up a connection to a MySQL database server - change the database_servername, database_username and password to suit: *******************************************************************************/
/******************************************************************************* Manage any errors that may have occured above. If a connection was NOT made, mysql_errno and mysql_error will output a reason why: *******************************************************************************/
if (!$dbconnect) { exit ("Error".mysql_errno()." : ".mysql_error().""); }
/******************************************************************************* With a connection now successfully made, choose the database to work with. Change the database name to suit your database:
*******************************************************************************/
mysql_select_db("name_of_the_database_to_work_with") or die ("Error ".mysql_errno()." : ".mysql_error()."");
/******************************************************************************* If this fails (dies), an error message will be output. If successful - the following will be displayed on screen: *******************************************************************************/
BEST PRACTICE TIPS -- The connection file is best placed outside your web directory, and then 'included' on the pages you need. That way, the password can easily be changed, and, *if* PHP ever stopped working on your host - your PASSWORD could not be exposed to a users web browser !!
include ('../path/to/connection.php');
*** Don't use mysql_error and mysql_errno on a production environment. ***
If for any reason there was an error, then the error messages displayed could pose a security risk.