Renaming a table in a MySQL database using PHP can be easily achieved with the ALTER TABLE SQL statement.
Using the Sql ALTER TABLE statement, it is possible to rename a MySQL tables name in PHP.
syntax:
ALTER TABLE old_table_name RENAME TO new_table_name
Below, we provide a detailed and modern approach to this process.
Create a PHP script to establish a connection to the database and execute the ALTER TABLE statement.
<?php
$dbhost = "localhost";
$dbuser = "your_username";
$dbpass = "your_password";
$dbname = "basename";
// Establishing the connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to rename the table
$sql = "ALTER TABLE members RENAME TO profiles";
// Execute the query and check for success
if ($conn->query($sql) === TRUE) {
echo "The table MEMBERS has been renamed to `profiles`.";
} else {
echo "Error renaming table: " . $conn->error;
}
// Close the connection
$conn->close();