Welcome Guest, Not a member yet? Register   Sign In
Dynamic Connection to Database using Array or DSN
#1

[eluser]Davvolun[/eluser]
Working on a (small-ish) project where a user will upload a data file to effectively create and populate a new database. (The uploading stuff is working like a champ, no problems there)

Code:
$dsn = array(
  'hostname' => 'localhost',
  'username' => $validUser,
  'password' => $validPass,
  'database' => $databaseName,
  'dbdriver' => 'mysql'
);

$this->load->dbforge( $dsn );

$documents = array( ... );

$this->dbforge->add_field( $documents );
$this->dbforge->add_key( 'document_id', true );
$this->dbforge->create_table( 'documents', true );

So, the create_table line fails with a "No database selected" error...I've tried this using a dsn string instead of the ci array format, and still no dice. The documentation (i believe) says that $this->load->dbforge($dsn), or for that matter, $this->load->database($dsn) should return an object reference to the database connection, but I'm not getting anything back from the load

Prior to this code section, the database gets created dynamically, but that seems to be working fine; afterwards I can connect to mysql via command line and see the database exists with no tables.

Definitely not a good day so far, I know it's got to be something simple I'm forgetting here, but for the life of me I can't figure out what.

Also, I can't seem to find anyone else doing something quite like this...this is a very trusted, non-"technical" (database tech anyway) user and the app is behind a password--am I way off base trying to do this?

I know I could drop everything into a single static database and give the tables prefixes and go on about my merry way, but for a number of reasons, that will just send a slightly different problem down the road and I wouldn't think that this would be so difficult anyway...everything else I've done with CI (uploading, e.g.) has been extremely easy and user-friendly.
#2

[eluser]Davvolun[/eluser]
No one?

Well in any case, here's how I managed to do it...

Code:
$dsn = 'mysql://' . $validUser . ':' . $validPass . '@localhost/' . $databaseName;

$this->load->database( $dsn );
$this->db->query('use ' . $databaseName );

$documents = array( ... );

$this->dbforge->add_field( $documents );
$this->dbforge->add_key( 'document_id', true );
$this->dbforge->create_table( 'documents', true );

Not sure why I need the query hack to connect to the database, but I guess it works.
#3

[eluser]Jagar[/eluser]
Check this post out it may help you:

http://ellislab.com/forums/viewthread/76151/
#4

[eluser]4ever[/eluser]
[quote author="Davvolun" date="1245268979"]No one?

Code:
$this->db->query('use ' . $databaseName );

Thanks a lot for this piece of code. Solved my problem.




Theme © iAndrew 2016 - Forum software by © MyBB