AmirHussein
AmirHussein

Reputation: 96

How to create multiple tables using one sql query

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.

Upvotes: 0

Views: 1714

Answers (2)

Tamil
Tamil

Reputation: 1203

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

Read more on multiple statements in the PHP Docs.

Upvotes: 1

O. Jones
O. Jones

Reputation: 108676

How can I send every queries in one?

You can't.

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

Upvotes: 0

Related Questions