使用SQL语句重命名MySQL表

I want to rename an existing table using SQL statement:

I have 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 3 statements I'm always getting 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!

Try using backquotes instead, e.g.:

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

Try it without the quotes so the final query will look like:

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

Hope this helps.

Have you connected to the server properly?

Have you selected the db the table is in?

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

mysql_query("ALTER TABLE table_name RENAME TO new_table_name");

The mysql query for rename table is
RENAME TABLE old_name TO new_name

appected answer 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!

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