Welcome Guest, Not a member yet? Register   Sign In
On the fly database setup

I'm attempting to set up my CI application to install itself on first run ala Wordpress. I'm having trouble trying to coneptualize a way of setting the database configuration on the fly during the installation process. Any ideas?

Hello? Is anybody out there?

how about checking for the database connection and if it fails on database server found but database not found, redirect to a controller/function that creates the database and the relevant tables.

If you are using MySQL, you may even be able to call a shell script to create the database


I already have the installation function in place, but I need a way to set the actual connection details (host, username, password) in config/database.php

So is this impossible?

[eluser]Rob W[/eluser]
Would the following work?

* Distribute the app with some specific values in the DB config ('demo_username' for example)
* Find & replace these values with the new ones
* Save the file

// Open the db config file - mode = read + write from beginning
$dbconfig_file = fopen('path/to/file', 'r+');
$dbconfig_text = fread($dbconfig_file);
// build arrays for str_replace to use
$find = array('demo_username') // values to find
$replace = array('real_username'); // values to replace, need to be in same position as those in $find
// Make the replacements
$new_dbconfig_text = str_replace($find, $replace, $dbconfig_text);
// rewrite the file
fwrite($dbconfig_file, $new_dbconfig_text);
// Close the handler

Obviously with better error checking - and you'll need to be very careful about validation on the provided options, otherwise somebody could inject some nasty raw PHP code into your config file.

Hope that helps...


How would you even be able to trap the connection error on autoload, though?

Still haven't found a solution to this yet. Do I really have to hack the core to trap the first run database error? I need to be able to set up host, username, and password in my installation controller. Any ideas?

The best example right now might be Derek's Bamboo Invoice. Check it out.

[eluser]Tom Schlick[/eluser]
you could always load the db values from the db Smile haha jk

Theme © iAndrew 2016 - Forum software by © MyBB