Part of the EllisLab Network
   
 
Dynamic Connection to Database using Array or DSN
Posted: 08 June 2009 12:30 PM   [ Ignore ]  
Summer Student
Total Posts:  4
Joined  05-15-2009

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)

$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.

Profile
 
 
Posted: 17 June 2009 11:02 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  4
Joined  05-15-2009

No one?

Well in any case, here’s how I managed to do it…

$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.

Profile
 
 
Posted: 17 June 2009 11:31 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  145
Joined  04-22-2009

Check this post out it may help you:

http://codeigniter.com/forums/viewthread/76151/

Profile
 
 
Posted: 15 May 2011 12:42 PM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  88
Joined  05-15-2011
Davvolun - 17 June 2009 03:02 PM

No one?

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

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

Profile