Search code examples
phpmysqlphpmyadmincreate-table

MySQL 'CREATE TABLE' Query Working Directly and Through PHPMyAdmin But Not Through PHP


I have a MySQL CREATE TABLE script that was generated by PHPMyAdmin. It works perfectly fine directly and through PHPMyAdmin, but I can't get it to work through my PHP script.

The error I'm getting is:

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 `vc5_users` (`id` int(12) NOT NULL AUTO' at line 14

Here's the query:

INSERT INTO `users` (fname, lname, email, username, password, books_available, users_available, signup_date)
VALUES (
  'John',
  'Doe',
  '[email protected]',
  'jdoe',
  'foobar',
  '250',
  '25',
  '123456' 
);

CREATE TABLE IF NOT EXISTS `jdoe_users` (
  `id` int(12) NOT NULL AUTO_INCREMENT,
  `fname` varchar(128) COLLATE utf8_bin NOT NULL,
  `created_at` varchar(32) COLLATE utf8_bin NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `jdoe_books` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `isbn` varchar(13) NOT NULL,
  `title` varchar(256) NOT NULL,
  `author` varchar(256) NOT NULL,
  `publisher` varchar(128) NOT NULL,
  `status` varchar(32) NOT NULL,
  `last_loan` date NOT NULL,
  `last_return` date NOT NULL,
  `bookholder` varchar(64) NOT NULL,
  `due_back` date NOT NULL,
  `quantity` int(11) NOT NULL DEFAULT '1',
  `thumnail_url` varchar(128) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Solution

  • Queries in PHP using mysql_query can only have 1 statement. You are trying to execute multiple at once.

    Seperate the statements and execute. It will work then.