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.

4
  • Does it return any errors? Commented Sep 2, 2016 at 11:11
  • 1
    Possible duplicate of Executing multiple SQL queries in one statement with PHP Commented Sep 2, 2016 at 11:11
  • @jitendrapurohit Every topics I saw was about other sql commands not CREATE TABLE. Commented Sep 2, 2016 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 Commented Sep 2, 2016 at 11:34

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

How can I send every queries in one?

You can't.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.