Connecting to a MYSQL database Tutorial

Made by: Nick

Before you can do anything with your MYSQL database you need to connect to it, which is what I am going to show you how to do in this tutorial.

For this I always use a few server side includes, one for a file I call the "config.php" file, one for the "opendb.php" file and one for the "closedb.php" file.

The config file should look something like this (of course changing the information to your mysql info):

<?php
$dbhost = 'mysql url';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'database_name';
?>


and the opendb file should be this:

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>


Both of these will be included at the top of any page we create that uses the database you have created.

You should also include a close database file at the end of each page you create which uses the database in question. This should look something like this:

<?php
mysql_close($conn);
?>

Feed the Dragon MySpace Tutorials