What? Are you trying to find a way that you can put the database connection stuff in one place instead of copying it into each file that needs it?
Two steps.
Step 1: Put the database credentials somewhere. For example, you could have a PHP script
Code: Select all
<?php
$cred = array(
"foo" => array(
"host" => "localhost",
"username" => "root",
"password" => "",
"database" => "com"
),
// ...
);
?>
It really, really doesn't matter how you store them. What does matter is whether someone could access it from the web: by using a PHP file it'll get executed instead of displayed to the user, but by using something else (such as a .txt file) anyone who browsed to it would see the information.
In that case just put the file somewhere not web accessible. If you have a public_html directory then put the file somewhere
not in there.
Step 2: Write a bit of code to grab the information from that file and use it to connect. If you used a PHP file (like above) called "cred.php" then you could have
Code: Select all
<?php
function connect($to) {
include "cred.php";
$connection = mysql_connect($cred[$to]["host"], $cred[$to]["username"], $cred[$to]["password"]);
mysql_select_db($cred[$to]["database"], $connection);
return $connection;
}
?>
Step 3 (I lied): Include that connect() function file whenever you need it, and call it like
But there are a billion other ways of doing this. Look around and find one you like.