0

Please help me to discover syntax error in my rename_table script. What i want is add date to the table name, but something goes wrong. Now here's the code:

$date = date('d-m-Y');
$query = "RENAME order TO order".$date;
if(mysql_query($ren)){
...
Alex
  • 11
  • 2
  • 5
    order is a reserved key word you need to backtick it – Abhik Chakraborty Nov 06 '14 at 11:42
  • In mysql There is the different syntax for Rename command. its `RENAME TABLE TAB_NAME TO NEW_TAB_NAME` – Ankit Bajpai Nov 06 '14 at 11:44
  • Please do not use `mysql_*` functions. They are officially deprecated. Refer to [this question.](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Vanitas Nov 06 '14 at 11:45
  • Thank you for your answers! The problem wasn't in backticks. Syntax error was the reason why the script did not execute. So only Ankit Bajpai's answer is right. – Alex Nov 06 '14 at 12:05

4 Answers4

1

You have to use backticks for order as it is a reserved keyword. Also you are executing the query wrongly.

if(mysql_query($ren))          
                 ^

Replace $ren with $query as your query is stored in a variable $query, not $ren..

So try with

$query = "RENAME TABLE `order` TO order".$date;
if(mysql_query($query))
Jenz
  • 8,280
  • 7
  • 44
  • 77
1

change

$query = "RENAME order TO order".$date;

to

$query = "RENAME `order` TO `order".$date."`";
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
0

You cant use - sign as table name, use _ or dmy format 06nov2014

Dil Dilshan
  • 361
  • 1
  • 4
  • 17
0

Try this

$date = date('d-m-Y');
$query = "RENAME `order` TO `order".$date."`";
if(mysql_query($ren))
Umair Ayub
  • 19,358
  • 14
  • 72
  • 146