...

View Full Version : numeric table names



rpjd
04-21-2011, 07:50 PM
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:

$Ref = (string)$_SESSSION['Ref'];
then creating the table like so

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.

rpjd
04-21-2011, 08:18 PM
I created a table called '2' in mysql console using:

create table `2` (column1, column2,.....);
How can I do this using php if the table name is $var?

Old Pedant
04-21-2011, 08:23 PM
$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.

rpjd
04-21-2011, 10:11 PM
I tried your solution Old Pendant but it did't work got this errror
Incorrect Table name.

oracleguy
04-21-2011, 11:16 PM
$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.

Old Pedant
04-21-2011, 11:16 PM
<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

echo "<hr>DEBUG SQL: " . $sql . "<hr>";

??

Old Pedant
04-21-2011, 11:17 PM
Great minds not only run in the same gutter, they run at the same speed. <grin/>

rpjd
04-21-2011, 11:54 PM
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!

Keleth
04-22-2011, 05:59 AM
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?

rpjd
04-22-2011, 09:46 AM
Got it working.

$sql = "create table `$var` (colum names(types))";

Old Pedant
04-22-2011, 08:17 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum