I was trying to dynamicly create a database and then create table in that database
// Create DB
DB::connection('mysql-group')->statement(
'CREATE DATABASE IF NOT EXISTS `' . $new_db_name . '`'
);
// Change DB
Config::set($prefix . '.database', $new_db_name);
DB::connection('mysql-group')->setDatabaseName($new_db_name);
DB::connection('mysql-group')->reconnect();
Log::debug(DB::connection('mysql-group')->getDatabaseName());
// Create Table
$table_schema = file_get_contents(base_path('resources/table_schema.sql'));
$split = explode("# --
", $table_schema);
foreach( $split as $sth ) {
DB::connection('mysql-group')->select(DB::raw($sth));
}
The file table_schema.sql look like
CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`eid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `eid` (`eid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# --
CREATE TABLE IF NOT EXISTS `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`eid` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `eid` (`eid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# --
But the problem is, the error kept comming out:
SQLSTATE[HY000]: General error: 2053 (SQL: CREATE TABLE IF NOT EXISTS `table` (`id` int(11) NOT NULL AUTO_INCREMENT, `eid` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `eid` (`eid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8; )
I tried adding this in the front, but the result is same
DB::connection('mysql-group')->statement(
'SET SESSION wait_timeout=65535'
);
Any suggestion?