5

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!

2
  • Try: "RENAME TABLE $renameFolder TO $newName"
    – acanimal
    Mar 3, 2012 at 19:46
  • WARNING: mysql_query is an obsolete interface and should not be used in new applications as it's being removed in future versions of PHP. A modern replacement like PDO is not hard to learn. If you're new to PHP, a guide like PHP The Right Way can help explain best practices.
    – tadman
    Feb 2, 2015 at 16:28

7 Answers 7

6

Try using backquotes instead, e.g.:

mysql_query( "RENAME TABLE `" . $renameFolder . "` TO `" . $newname . "`" );
3
  • I think it is that. logically we have to warp table name betwin ` not '.
    – artragis
    Mar 3, 2012 at 19:47
  • I tried it on my own set up and removing the quotes fixed it. I suspect that you have non-alphanumeric characters in your table name. Try it with ` quotes instead as atragis said.
    – Overv
    Mar 3, 2012 at 19:52
  • do you mean like that: mysql_query("RENAME " . '$renameFolder' . " TO ". '$newName') ? Mar 3, 2012 at 19:56
2

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

2

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!

1

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");
2
  • Yes the table is in. i aready used this statement and is not working for me. Mar 3, 2012 at 19:52
  • Then you have to put `` around the table names or concatenate the strings with the variables as suggested by Overv.
    – Liftoff
    Mar 3, 2012 at 19:55
1
RENAME TABLE `jshop`.`mob_apple` TO `jshop`.`item_mobile`;
1

With mysqli object oriented interface you can use the following to rename your table

 $sql = "RENAME TABLE OldName TO NewName";
//if the query is successful
if ($conn->query($sql) === TRUE) {
echo "Table  renamed successfully";
}
//If something goes wrong

 else {
 echo "Error creating table: " . $conn->error;
 }
0

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

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

Hope this helps.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.