Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Creating a database and more than one table (problem)

    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.

    Code:
    <?php
    $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";
      }
    else
      {
      echo "Error creating database: " . mysql_error();
      }
    
    // Create table colleague
    mysql_select_db("uafmaster", $con);
    $sql = "CREATE TABLE colleague
    (
    id int not null auto_increment,
    PRIMARY KEY(id),
    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
    mysql_query($sql,$con);
    
    // Create table main
    mysql_select_db("uafmaster", $con);
    $sql = "CREATE TABLE main
    (
    id int not null auto_increment,
    PRIMARY KEY(id),
    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
    mysql_query($sql,$con);
    
    mysql_close($con);
    
    ?>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    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:
    Code:
        `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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    leszer (09-27-2011)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I knew it was going to be something simple. lol Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •