Skip to content
Advertisement

How to write multiple queries in a single tables.sql file and upload it using PHP?

All I want is to create 2 or more tables using tables.sql file via PHP.

PHP create_db.php

DB_HOST, DB_USER, DB_PASSWORD, DB_NAME are defined in config.php file

PHP v7.4.7

SQL tables.sql

If I upload this tables.sql file directly to MySQL server using command line it is working.

MySQL v8.0.20 MySQL Comunity Server – GPL

Error message

Error: 1064, 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 ‘CREATE TABLE IF NOT EXISTS status ( status varchar(100) NOT NULL, `IDt’ at line 16

Advertisement

Answer

mysqli::query only accepts single query https://www.php.net/manual/en/mysqli.query.php.

If you want to execute multiple queries at once you have to use mysqli::multi_query https://www.php.net/manual/en/mysqli.multi-query.php.

Example:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement