How to create a set of tables in the database via PHP

1

I'm having trouble while creating the database automatically via PHP. I am developing a project in MVC from college and wanted to import the sql file and through a query create the tables.

In the previous part of the code is made the connection with the database and also the creation of the database also via query and both work perfectly; but at the time of creating the tables nothing happens nor errors, the code is as follows:

$sql = file_get_contents(BASE_URL."database/arquivo.sql");
echo $sql;
$this->connection->multi_query($sql);

I have tried with query() and multi_query() and nothing happens, what could be wrong?

Well, the sql file link is this: link

is indicating the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_' at line 12 

If I delete this line, the error continues to the following lines, I can not resolve.

    
asked by anonymous 25.11.2014 / 12:21

1 answer

1

I have tested here and all tables have been created even with the comments / headers in the file. The only problem was that in all tables the primary key is id so that this column does not exist in any table. Then you fix the names.

obs: suppress some table fields leaving just the

CREATE TABLE IF NOT EXISTS 'administrador' (
  'id_admin' int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Estrutura da tabela 'apresentacao'
--

CREATE TABLE IF NOT EXISTS 'apresentacao' (
  'id_apresentacao' int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

The test code was as follows:

$sql = file_get_contents('sql.sql');

if(!$db->multi_query($sql)){
    echo $db->error;
}else{
    echo 'tabelas criadas com sucesso';
}

Use multi_query () to process multiple sql statements at one time, remembering that they must be separated by semicolons.

    
25.11.2014 / 13:41