• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

php - Create table from variable

Hi E's, I try to create a table, but if I use a variable to give a name of table, the create table don't work.
In practice, if I write the below code, php create the table:
mysql_query("CREATE TABLE nome(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id))")
    or die(mysql_error());

But if I write the below code, php don't create the table:
mysql_query("CREATE TABLE $nome(
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id))")
    or die(mysql_error());

The difference is in line1, can be nome or $nome.
What treatment I have to do in variable $nome for php create the table?

The best regards, JC
0
Pedro Chagas
Asked:
Pedro Chagas
2 Solutions
 
johanntagleCommented:
Just a guess - add a space after $nome - it might be evaluating $nome( instead of just $nome
0
 
Dave BaldwinFixer of ProblemsCommented:
What are you setting the value of "$nome" to?  And what error message are you getting?
0
 
Sandeep KothariProject LeadCommented:
you need to assign value to $nome to get it work...
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
Pedro ChagasWebmasterAuthor Commented:
Hi, the error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1000 ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id))' at line 1

I update my code for:
mysql_query("CREATE TABLE $nome (
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id))")
    or die(mysql_error());
//the change was I create a space between variable and (

Curious is my variable $nome have the value of 1000, but if I change them for for example test, the script work. Can I create tables with just numbers in the name?

What I have to do now for this work?
0
 
Dave BaldwinFixer of ProblemsCommented:
See http://dev.mysql.com/doc/refman/5.0/en/identifiers.html

"Identifiers may begin with a digit but unless quoted may not consist solely of digits."

Use backticks "`".

mysql_query("CREATE TABLE `$nome` (
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id))")
    or die(mysql_error());
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try this
mysql_query("CREATE TABLE `$nome` (
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id))")
    or die(mysql_error());

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now