0

I'm going to prepare an installation script for my website which is automatically creates database and it's tables. How can I send every queries in one? Here is the PHP code:

$table_query = "CREATE TABLE IF NOT EXISTS ".TP."_usertypes(
                utid INT(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (utid),
                utname VARCHAR(255)
                )
                DEFAULT CHARACTER SET utf8
                COLLATE utf8_general_ci;

                CREATE TABLE IF NOT EXISTS ".TP."_users(
                uid INT(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (uid),
                utid INT(11) NOT NULL,FOREIGN KEY (utid) REFERENCES ".TP."_usertypes(utid) ON DELETE RESTRICT,
                username VARCHAR(255), 
                password VARCHAR(255),
                avatar VARCHAR(255)
                )
                DEFAULT CHARACTER SET utf8
                COLLATE utf8_general_ci";
mysqli_query($connection,$table_query) or die(mysqli_error($connection));

Thank you.

AmirHussein
  • 96
  • 1
  • 9
  • Does it return any errors? – P.Yntema Sep 02 '16 at 11:11
  • 1
    Possible duplicate of [Executing multiple SQL queries in one statement with PHP](http://stackoverflow.com/questions/13980803/executing-multiple-sql-queries-in-one-statement-with-php) – jitendrapurohit Sep 02 '16 at 11:11
  • @jitendrapurohit Every topics I saw was about other sql commands not CREATE TABLE. – AmirHussein Sep 02 '16 at 11:32
  • @P.Yntema `You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS cando_users( uid INT(11) NOT NULL AUTO_INCREMENT' at line 9` – AmirHussein Sep 02 '16 at 11:34

2 Answers2

1

With mysqli you're able to use multiple statements for real using mysqli_multi_query().

Read more on multiple statements in the PHP Docs.

Tamil
  • 1,193
  • 9
  • 24
0

How can I send every queries in one?

You can't.

You need to send each query in a separate php call.

O. Jones
  • 103,626
  • 17
  • 118
  • 172