6

I am trying to create more then one MySQL tables using php but it gives errors and i can not sort out what is wrong with it.

PHP code :

$sql = ' 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`) 
 ); 

 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`) 
  ); ';

  mysql_query($sql);

It gives the error, besides do not worry about the mysql connection. I properly connect with the DB and i have tested it, it is definitely some thing wrong with syntax.

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 second_table ( user_id INT

S. A. Malik
  • 3,465
  • 6
  • 37
  • 56
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Naftali Dec 24 '12 at 16:48
  • Technical differences aside, PDO is much easier to work with. – Brad Koch Dec 24 '12 at 16:49
  • 3
    Thanks for a vote down, but the CMS i am working supports mysql_* functions. I do not understand why people here are so fond of down votes when they do not even have some thing to use in the heads. – S. A. Malik Dec 24 '12 at 16:55
  • Are you all sure this has not been asked before? This sounds a bit like a questions that has been asked. Also @backTangent what is the Mysql error number? Please provide it. – hakre Dec 24 '12 at 17:10
  • Even it comes late, this might be more helpful: possible duplicate of [PHP: multiple SQL queries in one mysql\_query statement](http://stackoverflow.com/questions/345637/php-multiple-sql-queries-in-one-mysql-query-statement) – hakre Dec 24 '12 at 17:12
  • I did not knew that the error was due to multiple SQL queries, otherwise i would not have asked it. – S. A. Malik Dec 24 '12 at 17:32
  • Sure it's possible if you skip mysql_query and instead use for example mysqli_query. Check the answers here that is for any multi query (not just create table) http://stackoverflow.com/questions/16591831/is-there-a-smart-way-to-run-multiple-queries-in-mysql-separated-by-a-semicolon/ . – K. Kilian Lindberg May 16 '13 at 16:47

2 Answers2

4

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.

Salman A
  • 262,204
  • 82
  • 430
  • 521
2

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Naftali
  • 144,921
  • 39
  • 244
  • 303