Jump to content

database prefix's


madivad

Recommended Posts

I run several server's around the world and each one has me as a different username, and as such the mysql databases get these usernames as a default prefix. So when I want to mirror some code, I try to set a prefix variable based on the site name (http_host)... If I dump the data, all appears ok, but it never seems to work and I have ot hardcode the database name into each site.

 

Best illustrated by way of example:

 

For argument sake I have four domains and a testing server at home. Databse prefix's follow the -->:

 

1) site1.com            --> "site1_"
2) website2.net         --> "web2_"
3) anothersite3.com     --> "max_"
4) lastsite.com         --> "max"
5) test.com             --> "" 

test.com is a test server I have running on my home network and access through the local domain controllers and/or hosts file

 

I have the following code entered in an include file that is in the header of ever page:

 

$thissite = $_SERVER['HTTP_HOST'];  // I use this variable throughout the website, saves typing http_host all the time 

$__Prefix = array(
   "site1.com"        => "site1_",
   "website2.net"     => "web2_",
   "anothersite3.com" => "max_",
   "lastsite.com"     => "max_",
   "test.com"         => "" )

$dbPrefix	=	$__Prefix['$thisSite'];  
         // therefore searching through the array using the sitename 
         // as a key and fetching the correct prefix for that server
         // NOTE: if I dumo out dbPrefix here, it is correct!!!

$db_hostname = "localhost";
$db_database = $dbPrefix . "maxy";      // the database is called this on every server
...
          // but when dumping out $db_database, I get "maxy"!!!!

 

Now, every call to $db_database 'should' reference the sites respectively

1) site1_maxy

2) web2_maxy

3) max_maxy

4) max_maxy

5) maxy (at home, no prefix on the file)

 

Here is the problem, at home it all works great, but once uploaded to the server, I get missing database errors and upon dumping vars, I find that db_database name is missing the prefix.

 

I can do tests for the siteurl and it is correct eg, on site1

if ($thissite= site1.com) {echo "yep";}

 

print's 'yep' for me, yet the database name is only set to maxy and not site1_maxy I you would expect.

 

it is not an error with the http_host returning in("http://site.com","www.site1.com","site1.com/") or any other combination. I have confirmed this

 

For some reason it just doesn't work. I use the syntax 

$var1 = $var2 . "text"

 

elsewhere, and it always works, but not with the dbname.

 

I have pulled my hair out over this one. Does anyone have any ideas?

Link to comment
https://forums.phpfreaks.com/topic/41251-database-prefixs/
Share on other sites

have you tried echoing the output of $thissite?

 

your test: if ($thissite= site1.com) {echo "yep";} isnt valid

 

this will test to see if php can define $thissite as site1.com, not if that is its current value

 

should be, if ($thissite == site1.com) {echo "yep";}

Link to comment
https://forums.phpfreaks.com/topic/41251-database-prefixs/#findComment-199853
Share on other sites

why not just:

 

switch( $_SERVER['HTTP_HOST'] )
{

  case 'www.site1.com':
    $db_hostname = "site1";
    $db_database = "my_db_name_1";  
    break;

  case 'www.site2.com':
    $db_hostname = "site2";
    $db_database = "my_db_name_2";  
    break;
}

 

Sounds like you are trying to do something too complicated.

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/41251-database-prefixs/#findComment-199989
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.