View Full Version : Creating a database and more than one table (problem)

09-27-2011, 10:20 PM
Can anyone tell me why this isn't working? I feel it's going to be something dumb I just don't see.. but that's normal. lol

THe database gets created and the first table but I can't get it to cascade through the rest of the tables.

$con = mysql_connect("localhost","root","");
if (!$con)
die('Could not connect: ' . mysql_error());

// Create database
if (mysql_query("CREATE DATABASE uafmaster",$con))
echo "Database created";
echo "Error creating database: " . mysql_error();

// Create table colleague
mysql_select_db("uafmaster", $con);
$sql = "CREATE TABLE colleague
id int not null auto_increment,
Date date,
fname varchar(15),
lname varchar(15),
mname varchar(15),
pname varchar(15),
city varchar(15),
bphone varchar(10),
netid varchar(15),
email varchar(20),
super varchar(25),
psoft varchar(10),
status varchar(10),
exp varchar(12),
role varchar(20)


// Execute query

// Create table main
mysql_select_db("uafmaster", $con);
$sql = "CREATE TABLE main
id int not null auto_increment,
colleague-id int not null,
INDEX (colleague-id),
net varchar(5),
email varchar(5),
internet varchar(15),
aconf varchar(5),
pin varchar(10),
aplant varchar(5),
eruser varchar(5),
guser varchar(5),
suser varchar(5),
stuser varchar(5),
dsuser varchar(5),
other1 varchar(25),
kronos varchar(5),
pview varchar(5),
unix varchar(5),
other2 varchar(25)

// Execute query



Old Pedant
09-27-2011, 10:40 PM
colleague-id will be seen by *any* database as "colleague MINUS id".

If you must use non-standard characters in field names, then you must escape the names to tell the DB that you are doing so.

In MySQL, you use backticks to do so:

`colleague-id` int not null,
INDEX (`colleague-id`),

(The backtick usually shares the key with the ~ tilde.)

The rules for field names in DBs, in general, are the same as those for variable names.

Must start with a letter. Must contain only letters, digits, and underline characters. Must not be a keyword in that particular DB.

If you violate any of that, you need to escape the field name.

The easiest solution is *probably* to use an underline instead of a minus sign, but if you don't mind the backticks you can use the minus sign.

09-27-2011, 11:18 PM
I knew it was going to be something simple. lol Thanks!