Runtime database connection details setup |
[eluser]Unknown[/eluser]
Hello, How can I access the database connect variables in one of my controllers? Something like /config/database.php, the reason for this is I must access multiple databases and don't know the connection details until runtime. I could write a simple static php that reads the details, outputs in the /config/database.php and starts the CI application but that's really ugly coding, I'd like something like: -no db autoconnect in config. -controller1 reads the connection details: host, db, user, pass and stores in session -controller2,3,4... reads from session and does something like: $this->db->config->host= $this->db->config->user= etc and then $this->db->connect(); I don't know the number of DBs involved so multiple profiles in database.php is not an option. Thank you!
[eluser]theprodigy[/eluser]
According to the database section of the user_guide Quote:To connect manually to a desired database you can pass an array of values: Just fill the config array with the necessary values at runtime, then pass it into the load->database call. To connect to multiple databases, just set some variables equal to the load->database call, and pass in TRUE for the second parameter (it's listed in the same section of the user guide) Quote:If you need to connect to more than one database simultaneously you can do so as follows: Use these two in techniques together, and come up with something like: Code: $config['hostname'] = "localhost"; If you need to be connected to both databases at the same time, make sure to leave pconnect set to false, otherwise you may run into issues. |
Welcome Guest, Not a member yet? Register Sign In |