3

I have a functions.php file that has one PDO Connection i use to access the database and all that good stuff it's this one:

$host = "localhost";
$db = "maindatabase";
$adminuser = "admin";
$adminpw = "123456";
global $connection;

$connection = new Connection($host,$adminuser,$adminpw,$db);

And i require this script on every file that needs to access to the database, the thing is in some cases i want to change from $db = "maindatabase"; to $db = "anotherdb"; and i know i can do it with just a setter

$connection->setDatabase("anotherdb");

But since i require the functions.php file into every other file i don't know if it will overwrite itself back to default, does anybody knows how can i change it and make it stay ?

EDIT

I have changed require to require_once in my code and since the PHP Documentation says if the file has already been required it will not add it again, do you think this will solve the issue ?

6
  • Does functions.php wrap this code up in any way? If this is in a class, you could make a call in each page script to a constructor, supplying the database name in each case. Anyway: this is the reason for using a framework of some kind - you can do initialisation stuff in a parent controller, or some other initialisation section, depending on a routing variable or some other testable condition.
    – halfer
    Jul 24, 2012 at 17:24
  • Yeah actually i made the class Connection which is the one functions.php it's referring to. Jul 24, 2012 at 17:27
  • That's not what I mean. I mean if you are including functions and creating a global object in there, then you've created a Connection that in some cases you won't use. It's best to run the code that creates connections once, depending one what database you want to connect to.
    – halfer
    Jul 24, 2012 at 17:29
  • i only include functions.php on the files that need it. besides the user will choose the database connection at the start of the system and it will work with that database from there on Jul 24, 2012 at 17:33
  • the user will choose the database connection at the start of the system and it will work with that database from there on - you mean stored in a session? That's fine, but it still doesn't answer the question as to how you'll arrange some pages to access one db, and have other pages access another db.
    – halfer
    Jul 24, 2012 at 17:37

3 Answers 3

2

This is the classic problem with global variables. Your program state is unpredictable. You can't know which database you're referring to.

Your solution is proper dependency injection.

Implement a factory class to generate the correct database connection on demand, and don't use global variables. That's the correct solution.

1
  • I wish i new about this before i started my project, thanks a lot anyways Jul 24, 2012 at 19:18
2

You may make two connections for every database

$host = "localhost";
$db1 = "maindatabase";
$db2 = "anotherdb";
$adminuser = "admin";
$adminpw = "123456";
global $connection1, $connection2;

$connection1 = new Connection($host,$adminuser,$adminpw,$db1);
$connection2 = new Connection($host,$adminuser,$adminpw,$db2);
3
  • Since the system it's going to be used by a lot of users don't think it's a good idea Jul 24, 2012 at 17:09
  • If the connections are persistent, I don't think this will add much overhead - have you measured the performance impact? (If no, then so long as your approach is modular, it will be easy to tweak later).
    – halfer
    Jul 24, 2012 at 17:21
  • 1
    @IsaacGonzalez: well, that's a big enough topic to warrant a question on its own. But (a) use ab or some similar testing tool, and (b) if you aren't maxing out a capable server yet, then don't worry about it.
    – halfer
    Jul 24, 2012 at 17:26
1

It may get ugly depending on your level of analness when it comes to pretty code, but why not specifiy the database prefix in your query if the MySQL user has access to both databases?

Example:

SELECT * FROM maindatabase.tablename WHERE this='that';

On your other function, do:

SELECT * FROM anotherdb.tablename WHERE this='that';

You can also do:

$database_name = 'maindatabase';
$query = "SELECT * FROM {$database_name}.table";

$database_name = 'anotherdb';
$query = "SELECT * FROM {$database_name}.table";

This way you can toggle your $database_name variable where needed.

1
  • Yeah i don't think that's a good idea, i have like a LOT of querys in my code and i think there must be an easier way to do this, than add if statements to every query Jul 24, 2012 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.