A better way to do it:
code:
<?php
// Connection info
$database['username'] = "username";
$database['password'] = "pass";
$database['database'] = "database";
$database['host'] = "localhost";
// Database connect code
$database['link'] = false;
function query($query) {
global $database;
if(!$database['link']) {
make_connection();
}
return mysql_query($query, $database['link']);
}
function make_connection() {
global $database, $talen;
$database['link'] = mysql_connect($database['host'], $database['username'], $database['password']);
if(!$database['link']) {
die("Could not connect to server " . $database['host']);
}
if(!mysql_select_db($database['database'], $database['link'])) {
die("Database \"" . $database['database'] . "\" wasn't found!");
}
}
?>
Include this in all your scripts (I always make an include file in which I include everything) and when you need to acces the database just do:
code:
$result = query("INSERT INTO ...");
The code will never make more then 1 database connection, wil never make a connection when you don't need it, AND it works with multiple connections.
Just be sure you doesn't use $database somewhere else in the script, and off course use query instead of mysql_query everytime.
Besides that, you can better use require instead of include, because you're sure your scripts need the database... So if the file doesn't exist your script doesn't need to be executed.