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
https://forums.phpfreaks.com/topic/43147-solved-creating-multiple-tables-at-once/
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';
?> 


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.

Also, try to avoid columns with name of "date" and use news_date or similar.

 

Even though it is a SQL type definition, MySQL is lenient in allowing it as a column name. If you subsequently have to migrate to another RDBMS you could have problems requiring db and code changes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.