homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Define Database Connection

Msg#: 4544941 posted 3:40 pm on Feb 12, 2013 (gmt 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.




10+ Year Member

Msg#: 4544941 posted 9:53 pm on Feb 12, 2013 (gmt 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.


WebmasterWorld Senior Member 10+ Year Member

Msg#: 4544941 posted 10:04 pm on Feb 12, 2013 (gmt 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"){


Msg#: 4544941 posted 9:43 am on Feb 13, 2013 (gmt 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);

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved