How to rename a mysql table in php?

1

What I want is to rename a table from php. I have tried with this:

mysql_query("RENAME TABLE ". $tablaNom ." TO '.$nombre.'") or die(mysql_error());

The previous line renames me the table but it puts a "." in the name and when I want to make a select it tells me that the table does not exist.

Likewise I have tried using the following line:

mysql_query("RENAME TABLE ". $tablaNom ." TO '".$nombre."'") or die(mysql_error());

But it says the following: "Incorrect table name".

I hope you can help me, and thank you in advance.

    
asked by A.Monreal 14.07.2016 в 20:15
source

2 answers

3

You're on the right track, you needed to define the backspace quotes: ''

mysql_query( "RENAME TABLE '" . $tablaNom . "' TO '" . $nombre. "'" ) or die(mysql_error());
    
answered by 14.07.2016 в 20:19
0

After a lot of struggle I managed to make it work, I just did not understand why it worked. What I did was remove the quotes, the periods and the backspace quotes of the second variable; that is, the one that contains the new name of the table.

mysql_query( "RENAME TABLE '" . $tablaNom . "' TO  $nombre " ) or die(mysql_error());

Anyway, thank you and I hope this answer will be of help to other people.

Greetings

    
answered by 15.07.2016 в 18:38