Welcome Guest, Not a member yet? Register   Sign In
Newbie trying to use the Database Forge Class
#1

[eluser]Jay Turley[/eluser]
Hi, all.

I'm new to codeIgniter, coming from the world of .NET and classic ASP. And I am much happier now Smile

I'm trying to use DBforge to (1) drop a database, (2) create a database, and (3) create some tables.

I have no problems with (1) and (2). However, after I have created the database, and I try to add a table, I get the following error message:

Quote:An Error Was Encountered
Error Number: 1102

Incorrect database name ''

SHOW TABLES FROM ``

I assumed this was due to the fact that initially, my database configuration (config/database.php) was

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

So I added a line to the table creation bit to set the default database, since it isn't loaded.

Code:
$this->database_model->create_database();
$db['default']['database'] = "mydb";

Basically, what I am trying to do is allow a first-time user to set up a database if it doesn't exist, and then to create the tables. Since the database doesn't exist, I can't set the default database in database.php.

I have a feeling this problem is due to my inexperience with the framework; perhaps I am approaching the problem in the wrong way?

Any help would be greatly appreciated.
#2

[eluser]Eric Barnes[/eluser]
I am not sure if this is the desired outcome but I would probably insert the database name in the config file then in dbforge use something like:
Code:
$this->dbutil->create_database($this->db->database);
#3

[eluser]Jay Turley[/eluser]
[quote author="suzkaw" date="1202263538"]I am not sure if this is the desired outcome but I would probably insert the database name in the config file then in dbforge use something like:
Code:
$this->dbutil->create_database($this->db->database);
[/quote]

Okay, good point.

Regardless, the problem I am encountering is that if I have a database setup in my configuration database.php file
Code:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "foo";
$db['default']['password'] = "bar";
$db['default']['database'] = "my_stupid_db";
$db['default']['dbdriver'] = "mysql";

then - if the database does not exist already - I can't load the dbutil class to check to see if the database exists, as I get an error:
Quote:An Error Was Encountered
Unable to select the specified database: my_stupid_db

However, if I leave the default database blank,
Code:
$db['default']['database'] = "";
$db['default']['dbdriver'] = "mysql";

I can connect to the database to check to see if the database "my_stupid_db" exists. However if it doesn't, and I then create it, my configuration is wrong, and now dbutil won't work because there is no default database specified.

After seeing what I have written, either (a) I am taking the wrong approach, or (b) I need to be able to force $db['default']['database'] to have the correct value somehow.
#4

[eluser]Eric Barnes[/eluser]
Ha Ha good point. :down:

The only other thing I can think of is to have them CHMOD 666 the database file and then after it creates the database write the values to it. Maybe someone else can chime with other options.
#5

[eluser]Jay Turley[/eluser]
[quote author="suzkaw" date="1202265457"]The only other thing I can think of is to have them CHMOD 666 the database file and then after it creates the database write the values to it.[/quote]

Sadly, that was my first thought... start up the application with form to collect data, CHMOD, write out the config, CHMOD back, and then woot!




Theme © iAndrew 2016 - Forum software by © MyBB