Welcome Guest, Not a member yet? Register   Sign In
Problem creating database tables with DB Forge
#1

[eluser]lifegiver36[/eluser]
Hi all,

I have a problem which I don't know if there is an easy solution for since I am pretty new to codeIgniter. I am using the dbforge class to create tables in my database. Here is the code i have:

$test = $this->dbforge->create_table('testTable',TRUE);

if ($test) {
echo "database table created";
} else {
echo "database table creation failed";
}

The problem is that if I run this script multiple times, it keeps coming back as TRUE. So no matter what, each time the if statement runs it echo's out "database table created", even though it did not actually create the table because add the "IF NOT EXISTS" clause by making the second parameter TRUE in the create_table method.

To be clear though, this script does create the table the first time I run it. It's just that for some reason the $test variable keeps holding a true value even though no table gets created if I run that script multiple times. I figured the $test variable should hold a false value since the create_table method is not actually creating a table after the first time I run the script.

Anyone have any idea what I am doing wrong?
#2

[eluser]WanWizard[/eluser]
This method returns the result of the database call, not whether or not the table was created.

If you add TRUE as the second parameter, the SQL command 'CREATE TABLE IF NOT EXISTS' executes successfully, hence the return value TRUE.

If you want to know if a table exists, use:
Code:
if ($this->db->table_exists('table_name'))
{
   // some code...
}
#3

[eluser]lifegiver36[/eluser]
Thanks a lot buddy, that solved my problem. :-)




Theme © iAndrew 2016 - Forum software by © MyBB