Welcome Guest, Not a member yet? Register   Sign In
database config file
#1

[eluser]cinewbie81[/eluser]
Hi all,

The following is my setup.php controller clas:

Code:
class Setup extends Controller {

function Setup()
{
  parent::Controller();
  $this->load->helper(array('url','form','setting'));
  $this->load->model('dbmodel', 'sql', TRUE);
  $this->load->database();

}
    
function index()
{
  $this->load->dbutil();
  if ($this->dbutil->create_database('payroll'))   {
      echo 'Database created!';
  }
}

}


And the following is my database.php config:

Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "password";
$db['default']['database'] = "payroll";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";

When function index() loaded, the following error message prompted out:
"Unable to select the specified database: payroll"

This error is because I bind the value 'payroll' to $db['default']['database'] in my config file. It can be easily fixed if i manually query 'Create Database payroll' in prior, but i don't want it to be this way cause i don't want all my client have to manually create the database first before they can use the system. Any solution ??
#2

[eluser]sergitin[/eluser]
hi
I think first you most do
Code:
$db['default']['database'] = "mysql";
and then, to change the value of
$db['default']['database'] to your database

or

you can have two DATABASE CONNECTIVITY SETTINGS in database.php
the first is with
Code:
$active_group = "default";
and the other with
Code:
$active_group = "other";
and when you create the database, then you change the $active_group
at the other definition of connectivity
#3

[eluser]Michael Wales[/eluser]
Yeah - you're going to have to connect to some database to start off with (or modify the the database library to prevent it automatically selecting a database).
#4

[eluser]cinewbie81[/eluser]
[quote author="sergitin" date="1192477719"]hi
I think first you most do
Code:
$db['default']['database'] = "mysql";
and then, to change the value of
$db['default']['database'] to your database

or

you can have two DATABASE CONNECTIVITY SETTINGS in database.php
the first is with
Code:
$active_group = "default";
and the other with
Code:
$active_group = "other";
and when you create the database, then you change the $active_group
at the other definition of connectivity[/quote]

hi, thx, but how do u change the value $db['default']['database'] to my database from a function??
#5

[eluser]sergitin[/eluser]
hi, sorry I'm late
look at the user_guide folder on C.I. package
/user_guide/database/connecting.html

in that page, it's explained how to do
two kind of connections
with a very good examples

I hope it works.
#6

[eluser]cinewbie81[/eluser]
Yeh the user_guide you mention show a good example how to sawitch between 2 "existing database" ..
my problem now is "how to create a new database in my system then only connecting to it" ..
sergitin and walesmd suggestion will work i suppose, but i don't actually know how to modify the value $db['default']['database'] from my other controller class .. do we have to open the config file then modify it manually which i think not user friendly enough for a system user Sad
#7

[eluser]sergitin[/eluser]
I give you the option of to change the value of
Code:
$db['default']['database']

the other option is to begin with one connection setting and then you have created the other database
then you can work with another connection variable and not to touch the original one...
OK??
#8

[eluser]cinewbie81[/eluser]
[quote author="sergitin" date="1192697313"]I give you the option of to change the value of
Code:
$db['default']['database']

the other option is to begin with one connection setting and then you have created the other database
then you can work with another connection variable and not to touch the original one...
OK??[/quote]

Thanks. It's working now .. The code as following:
Code:
<?php

class Setup extends Controller {

    function Setup()
    {    
        parent::Controller();
            $this->load->helper(array('url', 'form'));
    }


    function RunSetup()
    {
           //"default" is a group that point to mysql database
           $DB1 = $this->load->database('default', TRUE);
           $DB1->query('CREATE DATABASE NEWDB');

           $DB2 = $this->load->database('myDB', TRUE);
    }
}



Anyway, I've got another question.. I have

Instead of have to load the database group everytime, how can I use this $DB1 and $DB2 value in other class of my project ?? Hope someone can help. Thanks !




Theme © iAndrew 2016 - Forum software by © MyBB