creating database and tables

Discussion in 'PHP' started by newbie12345, May 14, 2010.

  1. #1
    im trying to create a database containing tables
    but for some reason it is only creating the database and no the tables in it
    im using php myadmin
    thanks in advance

    
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not establish connection: ' . mysql_error());
      }
    
    // Create database
    if (mysql_query("CREATE DATABASE heads ",$con))
      {
      echo "Database created <br>";
      }
    else
      {
      echo "Database could not be created: " . mysql_error();
      }
    
    mysql_select_db("heads", $con);
    $sql = "CREATE TABLE Heather(
    username VARCHAR( 20 ),
    firstname VARCHAR( 100 ),
    lastname VARCHAR( 100 ),
    birthday VARCHAR( 20 ),
    gender VARCHAR( 20 ),
    address VARCHAR( 100 ),
    password VARCHAR( 30 ),
    confirm_password VARCHAR( 30 ),
    email_address VARCHAR( 50 ))" or die (mysql_error());
    		echo "table created";
    
    mysql_query ($sql, $con);
    
    mysql_close ($con);
    
    
    
    
    
    
    ?>
    
    PHP:

     
    newbie12345, May 14, 2010 IP
  2. DrVillain

    DrVillain Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not really sure, but your last "or die" is on a string assignment.

    whats the output. does it get past "table created"
     
    DrVillain, May 14, 2010 IP
  3. newbie12345

    newbie12345 Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    the only thing im getting to do is create the database
    and nothing else

    the tables are not creating
    
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not establish connection: ' . mysql_error());
      }
    
    // Create database
    if (mysql_query("CREATE DATABASE intss",$con))
      {
      echo "Database created <br>";
      }
    else
      {
      echo "Database could not be created: " . mysql_error();
      }
    
    mysql_select_db("intss", $con);
    $sql = ("CREATE TABLE tea(
    user_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    username VARCHAR(20) NOT NULL,
    firstname VARCHAR(100) NOT NULL,
    lastname VARCHAR(100) NOT NULL,
    birthday VARCHAR(20) NOT NULL,
    gender VARCHAR(20) NOT NULL,
    address VARCHAR(100) NOT NULL,
    password VARCHAR(30) NOT NULL,
    confirm_password VARCHAR(30) NOT NULL,
    email_address VARCHAR(50),
    PRIMARY KEY (user_id)") or die (mysql_error());
    
    mysql_query ($sql, $con);
    
    mysql_close ($con);
    
    
    
    
    
    
    ?>
    
    PHP:
     
    newbie12345, May 14, 2010 IP
  4. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    I really don't see any error in this script! I ran this code and both the DB and tables were created successfully! Look at this screenshot....

    [​IMG]

    What version of MySQL are you using?? Btw, try this code
    <?php
    //Connect to MySQL
    $con = mysqli_connect("localhost", "root", "") or die('Error Connecting to MySQL server '.mysqli_error($con));
    
    //Query to Create the Database
    $query1 = "CREATE DATABASE heads";
    $result1 = mysqli_query($con, $query1) or die('Error Creating DataBase '.mysqli_error($con));
    echo "Database heads created";
    
    //Select the Database
    mysqli_select_db($con, "heads");
    
    //Query to create table inside the database
    $query2 = "CREATE TABLE heather(
    username VARCHAR( 20 ),
    firstname VARCHAR( 100 ),
    lastname VARCHAR( 100 ),
    birthday VARCHAR( 20 ),
    gender VARCHAR( 20 ),
    address VARCHAR( 100 ),
    password VARCHAR( 30 ),
    confirm_password VARCHAR( 30 ),
    email_address VARCHAR( 50 ))";
    $result2 = mysqli_query($con, $query2) or die('Error Querying Database '.mysqli_error($con));
    echo "Table Heather created";
    
    //Close the connection
    mysqli_close($con);
    ?>
    PHP:
    Also there is a die inside your string! Although it shouldn be causing any trouble... But still... Give the above code a try! If it gives any error copy it here! Also note that, in most of the webhosts, you won't be able to create databases using MySQL queries. You have to manually create them and then create specific user for the database using which you can connect...
     
    Last edited: May 14, 2010
    swashata, May 14, 2010 IP
  5. newbie12345

    newbie12345 Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    tanks alot i solved the Problem
     
    newbie12345, May 14, 2010 IP