Create Multiple MySQL tables using PHP

You cannot use semicolon separated queries with mysql_query, this function only allows one query at a time!

You must execute your statements separately:

mysql_query("
    CREATE TABLE IF NOT EXISTS `mod_reminder_entries` (
        `id` INT(10) NOT NULL AUTO_INCREMENT, 
        `user_id` INT(10) NOT NULL, 
        `entry_name` VARCHAR(255) NOT NULL, 
        `entry_value` INT(10) NOT NULL, 
        PRIMARY KEY (`id`), 
        FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
    )
") or die(mysql_error());

mysql_query("
CREATE TABLE IF NOT EXISTS `second_table` (
    `user_id` INT(10) NOT NULL, 
    `fieldstotal` INT(10) NOT NULL, 
    FOREIGN KEY (`user_id`) REFERENCES tblclients (`id`) 
)
") or die(mysql_error());

Or better, use mysqli_multi_query -- which means you must switch to mysqli.


Just do what the error message says.

You can only execute one query at a time with mysql_* but please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Tags:

Mysql

Php