Part of the EllisLab Network
   
 
Dynamic Connection to Database using Array or DSN
Posted: 08 June 2009 10:30 AM   [ 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 09: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 09: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
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 721, on January 06, 2010 09:38 AM
Total Registered Members: 115007 Total Logged-in Users: 66
Total Topics: 122440 Total Anonymous Users: 4
Total Replies: 647313 Total Guests: 504
Total Posts: 769753    
Members ( View Memberlist )