Welcome Guest, Not a member yet? Register   Sign In
Dynamically set up multiple array concurrent connections
#1

[eluser]bossbs[/eluser]
Hi guys,
I'm back for a quick hint on databases again..

I'll start explaining you my db web site structure and then comes the question:
in my website, I create a new DB with some information for every new registered user (we'll call it UserDB_01 for user1, ecc) and I store some other data like username, email and so on in another database (we'll call it CommonDB).

The problem is this:
once user1 logs in I verify that his credentials are valid against CommonDB and everithing works fine;
at this point "I have to switch" to UserDB_01 for other operations but I don't know how to solve this "switch".
Consider that once user2 logs in, I'll verify his credentials against CommonDB, but this time I'll have to switch on UserDB_02 for secondary operations, and so on for every other user.
In this way we can say that each user needs to connect with two databases: CommonDB and UserDB_XX.

I know that in the database.php file I can create different "database groups" and I'm trying to use them, but due to the fact the users may grow up quickly I want to have only 2 groups:
default -> which stores CommonDB data connection (for authentication)

AND

UserDbGroup -> which stores UserDB_XX generic data, and where I have to set up only the right database name (bold line below) to establish the right connection:

$db['UserDbGroup ']['hostname'] = Q_SQL_SERVER;
$db['UserDbGroup ']['username'] = Q_SQL_USER;
$db['UserDbGroup ']['password'] = Q_SQL_PASS;
$db['UserDbGroup ']['database'] = "";
$db['UserDbGroup ']['dbdriver'] = "mysql";
$db['UserDbGroup ']['dbprefix'] = "";
$db['UserDbGroup ']['pconnect'] = TRUE;

Ok,
is it clear? I hope but I don't think so..
anyway, suggestions and eventually clarifying questions are welcome!

PS:
please don't say me that this kind of architecture with multiple databases is wrong, I cant change it..
Thanks,

Pietro
#2

[eluser]WanWizard[/eluser]
How about a brief visit to the docs?

http://ellislab.com/codeigniter/user-gui...cting.html tells you exactly how you can connect to a database. You'll find the section "Manually connecting to a database" an interesting read...
#3

[eluser]bossbs[/eluser]
[quote author="WanWizard" date="1300766281"]How about a brief visit to the docs?

http://ellislab.com/codeigniter/user-gui...cting.html tells you exactly how you can connect to a database. You'll find the section "Manually connecting to a database" an interesting read...[/quote]

Umm, ok... I red it lots of time and it does not containt information I need.
Probably my question was not so clear, anyway WanWizard thanks for your reply but it was not necessary to be sarcastic...

Briefly (and I hope to be more clear this time), is there a way to change at runtime database group parameters as I can do with configuration parameters?

Thanks again.

Pietro
#4

[eluser]WanWizard[/eluser]
I was not being sarcastic, I just referred you to the manual (which most people can't be bothered to read) which explains clearly what methods there are to connect to a database.

Changing parameters at runtime (of an existing connection) is pointless, making a new connection at runtime (with parameters) is described in the user guide.
#5

[eluser]bossbs[/eluser]
Ok, no problem WanWizard.
I'll review my application trying to follow your suggestion even if it means for me to establish the connection multiple times ( or maybe I'll use the reconnect() command)

Thanks for your help!

Pietro
#6

[eluser]WanWizard[/eluser]
I'm still a bit clueless to what it is that you're trying to do. Could you explain what it is that you're trying to achieve?

You want to set all parameters in the config file, but determine the database name at runtime, and then make the connection?
#7

[eluser]bossbs[/eluser]
Umm, in a certain way.. I want to prepare two different database groups in the config file, then connect to the default DB for user validation and according to the user data, quickly connect to its database (each user has its own DB).
Anyway, I think that I solved it using the database array connection, but soon or later I wanna be back on this topic and analyze it in a better way.
Pietro
#8

[eluser]WanWizard[/eluser]
Ok, not too difficult.

This assumes that you have a user db definition in your config database.php called 'users'.
Code:
public function load_userdb($userdb)
{
    // load the database config
    include APPPATH.'config/database.php';

    // make sure we have our DB config
    if ( ! isset($db) || ! is_array($db) || ! array_key_exists('users', $db) )
    {
        // oops, bail out!
        die('WTF?');
    }
    else
    {
        // store the custom db name
        $db['users']['database'] = $userdb;

        // and make the DB connection
        $this->userdb = $this->load->database( $db['users'] );
    }
}
#9

[eluser]bossbs[/eluser]
WanWizard thanks!

In the last line you are using a variable that I've never seen in the previous lines:
$this->userdb
what is it? Did you mean $this->db?
Thanks again for your help




Theme © iAndrew 2016 - Forum software by © MyBB