-2

I'm trying to rename a table in a Mysql db, from PHP, but this code doesn't work:

$sql = "RENAME TABLE dbname.table TO dbname.tabletmp;"; 
$conn->query($sql);

I've tried to use also ALTER TABLE or the '' for the table names, but nothing. Any ideas?

It seems that in PHP ALTER TABLE isn't a command :/

piet.t
  • 11,718
  • 21
  • 43
  • 52
Giuse_07
  • 1
  • 1

2 Answers2

0

It seems that in PHP ALTER TABLE isn't a command :/

PHP doesn't care about SQL. Not even $conn->query() cares about it. You pass it a string and it passes it further to the server.

Make sure the user you use to connect has the necessary privileges to rename a table. The documentation of RENAME TABLE says:

When you execute RENAME TABLE, you cannot have any locked tables or active transactions. You must also have the ALTER and DROP privileges on the original table, and the CREATE and INSERT privileges on the new table.

axiac
  • 68,258
  • 9
  • 99
  • 134
-1

Try this

$sql = "RENAME TABLE `" . $oldname . "` TO `" . $newname . "`" ;
$conn->query($sql);
Sehdev
  • 5,486
  • 3
  • 11
  • 34