Jump to content

[SOLVED] Creating Multiple Tables At Once


Design

Recommended Posts

Ok.... so here is my db creation thing right now, I wanna add in 2 other tables to this database, what's the best way to do so?

<?php
include 'config.php';
include 'opendb.php';

$query  = 'CREATE DATABASE xbc';
$result = mysql_query($query);

mysql_select_db('xbc') or die('Cannot select database'); 

$query = 'CREATE TABLE users( ' .
	 ' (id int(10) DEFAULT '0' NOT NULL auto_increment, ' .
	 ' username varchar(40), ' .
	 ' password varchar(50), ' .
	 ' regdate varchar(20), ' .
	 ' email varchar(100), ' .
	 ' ip varchar(12), ' .
	 ' flag varchar(1), ' .
	 'PRIMARY KEY(id))';

$result = mysql_query($query);

include 'closedb.php';
?> 

 

Any help is greatly appreciated.

Link to comment
Share on other sites

yeah i noticed that and fixed it after i replied ^^

So this should work? :

<?php
include 'config.php';
include 'opendb.php';

$query  = 'CREATE DATABASE xbc';
$result = mysql_query($query);

mysql_select_db('xbc') or die('Cannot select database'); 

$query = 'CREATE TABLE users( ' .
	 ' id int(10) DEFAULT '0' NOT NULL auto_increment, ' .
	 ' username varchar(40), ' .
	 ' password varchar(50), ' .
	 ' regdate varchar(20), ' .
	 ' email varchar(100), ' .
	 ' ip varchar(15), ' .
	 ' flag varchar(1), ' .
	 'PRIMARY KEY(id))';

	 'CREATE TABLE news( ' .
	 ' id int(10) DEFAULT '0' NOT NULL auto_increment, ' .
	 ' author varchar(40), ' .
	 ' title varchar(40), ' .
	 ' content NOT NULL ' .
	 ' date varchar(20) ' .
	 'PRIMARY KEY(id))';

	 'CREATE TABLE maps( ' .
	 ' id int(10) DEFAULT '0' NOT NULL auto_increment, ' .
	 ' author varchar(40), ' .
	 ' title varchar(40) NOT NULL, ' .
	 ' type varchar(20), ' .
	 ' content MEDIUMBLOB NOT NULL, ' .
	 ' size NOT NULL, ' .
	 'PRIMARY KEY(id))';

$result = mysql_query($query);

include 'closedb.php';
?> 


Link to comment
Share on other sites

You need to execute them as three separate queries

 

<?php
<?php
$query = 'CREATE TABLE users( 
	  id int(10) DEFAULT \'0\' NOT NULL auto_increment, 
	  username varchar(40), 
	  password varchar(50), 
	  regdate varchar(20), 
	  email varchar(100), 
	  ip varchar(12), 
	  flag varchar(1), 
	 PRIMARY KEY(id))';
mysql_query($query);


$query = 'CREATE TABLE news( 
	  id int(10) DEFAULT \'0\' NOT NULL auto_increment, 
	  author varchar(40), 
	  title varchar(40), 
	  content NOT NULL 
	  date varchar(20) 
	 PRIMARY KEY(id))';
mysql_query($query);

$query = 'CREATE TABLE maps( 
	  id int(10) DEFAULT \'0\' NOT NULL auto_increment, 
	  author varchar(40), 
	  title varchar(40) NOT NULL, 
	  type varchar(20), 
	  content MEDIUMBLOB NOT NULL, 
	  size NOT NULL, 
	 PRIMARY KEY(id))';
mysql_query($query);


?> 

 

I escaped your quotes as you had single-quoted string inside a single-quoted string.

 

Also you can spread the query statement over several lines. No need for all the concatenation. With complex queries, if MySQL reports a syntax error it will tell you the line number, which helps to locate it. If it's all a single line it just reports line 1.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.