Help With Creating Data Table

Discussion in 'MySQL' started by zahidprimex, Aug 14, 2009.

  1. #1
    I am using this code below to create a table in database

    <?php
    	$id=$_POST['id'];
    	$con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    //Create Table For Catagory
    mysql_select_db("test",$con);
    $sql = "CREATE TABLE $id 
    (
    FirstName varchar(15),
    LastName varchar(15),
    Age int
    )";
    
    // Execute query
    mysql_query($sql,$con);   
    
    ?>
    Code (markup):
    It is not working as I used a variable as table name.How can I use a variable as table name when creating new table?
     
    zahidprimex, Aug 14, 2009 IP
  2. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It should work perfectly fine using $id. Change
    mysql_query($sql,$con);
    to
    $result = mysql_query($sql);

    If it still doesn't work, add in:
    echo $sql;
    and paste the resulting query here.
     
    Goramba, Aug 15, 2009 IP
  3. zahidprimex

    zahidprimex Active Member

    Messages:
    123
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Thanks for ur reply.But it's not working,The resulting query is

    CREATE TABLE details { FirstName varchar(15), LastName varchar(15), Age int }
    Code (markup):
     
    zahidprimex, Aug 16, 2009 IP
  4. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4

    Quoting the query always works. Get rid of the brackets and use parenthesis.

    NO: {
    YES: (
     
    Goramba, Aug 17, 2009 IP
  5. zahidprimex

    zahidprimex Active Member

    Messages:
    123
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    73
    #5
    It's working.Thank for your help.Reputation added.
     
    zahidprimex, Aug 17, 2009 IP