I have tried all sorts of things to get a table renamed from php, including mysqli (code below) and mysql_connect/mysql_query. I have tried rename table and alter table. In the example below there are no errors when I execute it. However, when I go look, the 'trac' table is still there with no '2' at the end.

$link = mysqli_connect("lamp1", "user", "pw", "db");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
if ($stmt = mysqli_prepare($link, "rename table trac to trac2")) {
 echo "error";

This is xampp (recent version) on a Windows Server 2008 machine. Mysql 5.0.7, php 5.3.4.

I can rename the table with no issue through phpMyAdmin!

What can I check / try? Is there a log file I can turn on / look at to see whats happening from the mysql side?

  • Are you getting "error" as a result of your script? Throw a echo mysqli_stmt_error($stmt); right after the execute (inside the if) – Aaron Aug 4 '11 at 20:08
  • Well, ultimately mysqli_stmt_error($stmt); solved it for me. I think between my different tests I may have had some 'table already exists' or other errors and now that I could see the errors, I was able to work it all out. Ultimately my code above was correct. – Scott Szretter Aug 5 '11 at 0:23

Use mysqli_stmt_error($stmt) to figure out the error that is being returned, and then act on that.


citing from the MySQL manual

When you execute RENAME, 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.

So it is very likely that the user user does not have the correct privileges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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