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" Commented 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. Commented Feb 2, 2015 at 16:28

7 Answers 7

6

Try using backquotes instead, e.g.:

mysql_query( "RENAME TABLE `" . $renameFolder . "` TO `" . $newname . "`" );
Sign up to request clarification or add additional context in comments.

3 Comments

I think it is that. logically we have to warp table name betwin ` not '.
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.
do you mean like that: mysql_query("RENAME " . '$renameFolder' . " TO ". '$newName') ?
2

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

Comments

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!

Comments

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 Comments

Yes the table is in. i aready used this statement and is not working for me.
Then you have to put `` around the table names or concatenate the strings with the variables as suggested by Overv.
1
RENAME TABLE `jshop`.`mob_apple` TO `jshop`.`item_mobile`;

Comments

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;
 }

Comments

0

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

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

Hope this helps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.