Design Posted March 17, 2007 Share Posted March 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 Add two more create table queries after the one you have now. Quote Link to comment Share on other sites More sharing options...
Design Posted March 17, 2007 Author Share Posted March 17, 2007 ok thx, I was just making sure that wouldnt conflict with the first 1. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 BTW, you have an extra "(" just before "id" column definition Quote Link to comment Share on other sites More sharing options...
Design Posted March 17, 2007 Author Share Posted March 17, 2007 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'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 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. Quote Link to comment Share on other sites More sharing options...
Design Posted March 17, 2007 Author Share Posted March 17, 2007 Thank you, Barand. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.