Usually you want to create a “config.php” file, a “opendb.php” file, and a “closedb.php” file.
In the config.php file:
<?php
// config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'databaseName';
?>
In the opendb.php file:
<?php
// opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
In the closedb.php file:
<?php
// closedb.php
// not needed, but can help
mysql_close($conn);
?>
Now, we include all 3 files like this:
<?php
include 'config.php';
include 'opendb.php';
// insert, update select, or delete statement goes here
include 'closedb.php';
?>