I usually create a file called _database.php or what not, and then have all the database connection functions in there, database abstraction functions if you want to make some, etc.
I also have a file included called _config.php that contains the database username/pass/dsn. That way I keep things even more separate.
here's something I usually use for postgresql, you can do something similar for mysql. Database abstraction obviously isn't necessary, but it was already built into this file I use. 🙂
--- begin _database.php ----
<?
/*
* *SIMPLE* database abstraction, nothing fancy, no retarded modules to
* install, just this file.
*/
$database = @pg_connect("user=$pg_user password=$pg_pass dbname=$pg_db");
if(!$database) {
echo 'database unavailable'; exit();
}
function db_query($sql, $noexit = 0) {
$result = @pg_exec($GLOBALS['database'],$sql);
$GLOBALS['_pg_err'][$result] = pg_errormessage($GLOBALS['database']);
$GLOBALS['_pg_data'][$result]=0;
if(!$noexit && $GLOBALS['_pg_err'][$result]) {
echo $GLOBALS['_pg_err'][$result] ."<br>"; exit();
}
return $result;
}
function db_error($result) {
return $GLOBALS['_pg_err'][$result];
}
function db_num_rows($result) {
return pg_numrows($result);
}
function db_affected_rows($result) {
return @pg_cmdtuples($result);
}
function db_fetch_object($result) {
return @pg_fetch_object($result, $GLOBALS['_pg_data'][$result]++);
}
function db_reset($result) {
$GLOBALS['_pg_data'][$result] = 0;
return;
}
function db_free_result($result) {
pg_freeresult($result);
return;
}
?>