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 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts

    numeric table names

    I want to create a table with numeric and partly numeric names such as '1' and '1-2'. Reading the mysql docs, tables can have numeric names as long as they are string literals. The table names are stored as a session array. I tried casting the values as string like this:
    PHP Code:
    $Ref = (string)$_SESSSION['Ref']; 
    then creating the table like so
    Code:
    newTable = "create table ('$Ref') (column names)";
    but this isn't working. Has anyone done this before? Am I going about this all wring? Any help guidance appreciated.

  • #2
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I created a table called '2' in mysql console using:
    Code:
    create table `2` (column1, column2,.....);
    How can I do this using php if the table name is $var?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Code:
    $sql = "CREATE TABLE `$var` ( column1 type1, column2 type2, ... )";
    But it's almost always indicative of a problematic DB design when you find yourself creating tables dynamically in PHP (or ASP or JSP or...) code. 95% or more of the time it means you should have found another way to do this.
    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.

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I tried your solution Old Pendant but it did't work got this errror
    Incorrect Table name.

  • #5
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    $sql = "CREATE TABLE `$var` ( column1 type1, column2 type2, ... )";
    But it's almost always indicative of a problematic DB design when you find yourself creating tables dynamically in PHP (or ASP or JSP or...) code. 95% or more of the time it means you should have found another way to do this.
    Totally agree. Not to mention giving a table a numeric name which will be very much not descriptive of the data it contains.

    As for the OP, try echoing out your $sql variable after you've assigned it a value. That might help you figure out why the query is failing.
    OracleGuy

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    <shrug>well, I suppose it could be that the PHP mysql library does't allow it. I don't use PHP, so dunno.

    Did you debug??

    For example, did you do
    Code:
        echo "<hr>DEBUG SQL: " . $sql . "<hr>";
    ??
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Great minds not only run in the same gutter, they run at the same speed. <grin/>
    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.

  • #8
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If it works in the mysql console, I can't see why it would work with php, possibly, probably different syntax. At least I hope!

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    PHP doesn't have a MySQL syntax. It acts as the console TO MySQL. So if it worked in the MySQL console, it should work with PHP. Like Old Pedant said, did you echo the query? Did it show as expected?

  • #10
    Regular Coder
    Join Date
    Apr 2007
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got it working.
    Code:
    $sql = "create table `$var` (colum names(types))";

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    How is that different from what I gave you???

    Oh, well...

    Now can you explain *WHY* you are doing this? I still think it shows the likelihood of a bad DB design.
    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.


  •  

    Posting Permissions

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