0

I want to create some table through PHP but it fails everytime, but the same code excutes perfectly when excuted through MYsql console or PHPMyAdmin

The SQLs are

$sql = <<<SQL_CODE
CREATE TABLE IF NOT EXISTS `bid` (
  `aid` int(11) unsigned NOT NULL,
  `uid` int(11) unsigned NOT NULL,
  `name` varchar(20) NOT NULL,
  `amount` smallint(6) unsigned NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS `item` (
  `aid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `description` varchar(120) NOT NULL,
  `img` int(11) unsigned NOT NULL,
  `amount` smallint(6) unsigned NOT NULL,
  `strtdate` date NOT NULL,
  `enddate` date NOT NULL,
  `uid` int(11) unsigned NOT NULL,
  `uname` varchar(20) NOT NULL,
  `uamount` int(11) unsigned NOT NULL,
  PRIMARY KEY (`aid`)
);

CREATE TABLE IF NOT EXISTS `user` (
  `uid` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(40) NOT NULL,
  `password` varchar(40) NOT NULL,
  `nameF` varchar(20) NOT NULL,
  `nameL` varchar(20) NOT NULL,
  `sex` varchar(1) NOT NULL,
  `img` int(11) unsigned NOT NULL,
  `country` varchar(10) NOT NULL,
  `state` varchar(20) NOT NULL,
  `address` varchar(120) NOT NULL,
  `code` varchar(8) NOT NULL,
  `isAdmin` varchar(1) NOT NULL,
  PRIMARY KEY (`uid`),
  UNIQUE KEY `email` (`email`)
);
SQL_CODE;

$sql2 = 'CREATE DATABASE `'.$db.'`;';
$sql3 = 'USE `'.$db.'`; ';
$sql4 = 'drop Database `'.$db.'``;';

if (!mysql_query($sql2)) echo mysql_error();
if (!mysql_query($sql3)) echo mysql_error();
//sleep(1);
if (!mysql_query($sql)) echo mysql_error();
echo $sql2.' '.$sql3.' '.$sql;

Error I'm getting

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 item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT,' at line 1CREATE DATABASE auction; USE auction; CREATE TABLE IF NOT EXISTS bid ( aid int(11) unsigned NOT NULL, uid int(11) unsigned NOT NULL, name varchar(20) NOT NULL, amount smallint(6) unsigned NOT NULL, time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); CREATE TABLE IF NOT EXISTS item ( aid int(11) unsigned NOT NULL AUTO_INCREMENT, name varchar(40) NOT NULL, description varchar(120) NOT NULL, img int(11) unsigned NOT NULL, amount smallint(6) unsigned NOT NULL, strtdate date NOT NULL, enddate date NOT NULL, uid int(11) unsigned NOT NULL, uname varchar(20) NOT NULL, uamount int(11) unsigned NOT NULL, PRIMARY KEY (aid));CREATE TABLE IF NOT EXISTS user ( uid int(11) unsigned NOT NULL AUTO_INCREMENT, email varchar(40) NOT NULL, password varchar(40) NOT NULL, nameF varchar(20) NOT NULL, nameL varchar(20) NOT NULL, sex varchar(1) NOT NULL, img int(11) unsigned NOT NULL, country varchar(10) NOT NULL, state varchar(20) NOT NULL, address varchar(120) NOT NULL, code varchar(8) NOT NULL, isAdmin varchar(1) NOT NULL, PRIMARY KEY (uid), UNIQUE KEY email (email));
2
  • $db is some valid database name Commented Mar 17, 2012 at 16:14
  • 1
    Maybe you can't execute multiple CREATE TABLE in one mysql_query call? Commented Mar 17, 2012 at 16:18

3 Answers 3

2

From the mysql_query manual;

mysql_query() sends a unique query (multiple queries are not supported)...

You need to split your triple CREATE TABLE statement into 3 separate statements and it will work.

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

Comments

1

I think that is a problem with multiple queries. I think that is solution.

1 Comment

Thats a mysqli but I was using mysql. +1 for your response
1

http://php.net/manual/en/function.mysql-query.php

*mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier*

Just split them up and it will work.

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.