Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Define Database Connection

3:40 pm on Feb 12, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 0


I'm trying to figure out how to set/define the database connection based on a mysql select. At present it runs the check on each page load which there is no need for.

//My database connection goes here

//Host Name
$domain = $_SERVER['HTTP_HOST'];

//Customer Check
$sql1 = "SELECT * FROM customers WHERE cust_domain = '$domain'";
$result1 = mysql_query($sql1) or die(mysql_error());
$num_rows1 = mysql_num_rows($result1);

//Set Customer Database Values
if ($num_rows1 > 0) {
while($row1 = mysql_fetch_row($result1)) {
$id = $row1['0'];
$name = $row1['1'];
$domain = $row1['2'];
$db_ip = $row1['3'];
$db_name = $row1['4'];
$db_username = $row1['5'];
$db_password = $row1['6'];
$customer_exists = true;

//Customer Database Connection
if ($customer_exists = true) {
$hostname = $db_ip;
$database = $db_name;
$username = $db_username;
$password = $db_password;
$connect = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database, $connect);

As you can see it checks for if the customer exists each time. Can anyone help me change the above so once it has checked it sets the customer database connection and does not check if they exist again please.

I know I'm missing lots of error checking etc but I just wrote this, once working I will add error checking etc.

9:53 pm on Feb 12, 2013 (gmt 0)

Junior Member

10+ Year Member

joined:June 24, 2004
votes: 0

You have a separate database for each customer? That's an unusual way of doing things. Normally, you would using a unique ID for each customer and use the ID as a key in tables of customer data. Having customer data in a database for each customer makes it so that you can't, for example, run any queries that return information about more than one customer at a time.

But ... if having a separate database for each customer is what you really want, you can't get around selecting the database each time.
10:04 pm on Feb 12, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 15, 2004
votes: 0

What jadebox said being true, and not knowing what exactly you are trying to achieve, you may store this in a session.

$_SESSION['customer_exists'] = true;

And change this:
if ($customer_exists = true) {


if ($customer_exists == true) {

If you are displaying different content based on the URL, you are probably better off having a config file and hard coding this stuff.

And then do something like this.

if ($_SERVER['HTTP_HOST'] == "thisdomain"){
} else if ($_SERVER['HTTP_HOST'] == "thatdomain"){
9:43 am on Feb 13, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Aug 3, 2010
posts: 243
votes: 0

Yeah I intend on having a seperate database for each customer. If I explain the setup hopefully that will clear things up. I'm developing a system which I will sell to multiple customers. The code will be hosted on my domain however the customers database will be hosted on their domain. The thinking behind this is each customers data is their own, it isn't mixed with everyone elses.

The reason I didn't go the way Habtom suggested is because there could be hundreds of config files, meaning allot of if statements.

The content will be different based on the url, only as the data available is dependant on the url/customer database connection.

The code I've posted works however it checks each time, which is not exactly ideal. I've looked into using DEFINE but I'm still not sure I can do it that way.

I think it's just this bit that needs changing

$hostname = $db_ip;
$database = $db_name;
$username = $db_username;
$password = $db_password;

Would something like this work?

DEFINE ("CUST_DB_IP", $db_ip);
DEFINE ("CUST_DB_NAME", $db_name);