Syntax error when putting a number as mysql table name

0

Hello, I'm doing the following query with the variable

$fechaActual = date('dmy');

dasd

$mysqli->query("CREATE TABLE ".$fechaActual." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY)")

This gives me syntax error but if I do this:

$mysqli->query("CREATE TABLE a".$fechaActual." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY)")

it works ... (add one to the variable)

pd: if I can create a table with only numbers like phpmyadmin

    
asked by Cristofer Fuentes 25.02.2017 в 06:44
source

1 answer

2

The problem is not that your tables are numbers. The problem is that they are only numbers.

An identifier (the name of a table is one) in MySQL should consist of the characters [0-9,a-z,A-Z$_] .

But:

"Identifiers may begin with a digit but unless quoted may not consist solely of digits." says ID documentation in MySQL

You have to escape the name of the table if you want to use pure numbers. To keep things simpler, I recommend another scheme to name your tables.

    
answered by 25.02.2017 / 07:03
source