Rename MySQL table with SQL statement

I want to rename an existing table using an SQL statement:

I already tried:

1)mysql_query("RENAME '$renameFolder' TO '$newName'"); 2)mysql_query("ALTER TABLE '$renameFolder' RENAME TO '$newName'"); 3)mysql_query("RENAME TABLE '$renameFolder' TO '$newName'"); 

Using any of the three im statements always gets the same error message:

 "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax" 

Please tell me what I'm doing wrong!

+4
source share
6 answers

Try using backquotes instead, for example:

 mysql_query( "RENAME TABLE `" . $renameFolder . "` TO `" . $newname . "`" ); 
+4
source

Logged response from mySQLi:

 $db=mysqli_connect("localhost","root","password","database"); $oldFolder="old_table_name"; $newname="new_table_name"; mysqli_query($db,"RENAME TABLE `" . $oldFolder . "` TO `" . $newname . "`"); 

Good luck

+2
source

Have you connected to the server correctly?

Did you choose db where the table is located?

If you have, you should be able to run this:

 mysql_query("ALTER TABLE table_name RENAME TO new_table_name"); 
+1
source

RENAME TABLE old_name TO new_name query for rename table - RENAME TABLE old_name TO new_name

+1
source

Try this without quotes, so the final request will look like this:

 mysql_query ("ALTER TABLE foo RENAME TO bar"); 

Hope this helps.

0
source
 RENAME TABLE `jshop`.`mob_apple` TO `jshop`.`item_mobile`; 
0
source

Source: https://habr.com/ru/post/1399571/


All Articles