• DevOps
    Case Study

    How we built a resilient multi-account, multi-cloud solution for a Health Tech service provider!

    READ CASESTUDY
    icon

    24/7 DevOps as a Service

    Round-the-clock DevOps for uninterrupted efficiency.

    icon

    Infrastructure as a Code

    Crafting infrastructure with ingenious code.

    icon

    CI/CD Pipeline

    Automated CI/CD pipeline for seamless deployments.

    icon

    DevSecOps

    Integrated security in continuous DevOps practices.

    icon

    Hire DevOps Engineers

    Level up your team with DevOps visionaries.

    icon

    Consulting Services

    Navigate success with expert DevOps consulting.

  • TechOps
    Case Study

    How we built a scalable Odoo solution for a Travel Tech service provider!

    READ CASESTUDY

    WEB HOSTING SUPPORT

    icon

    HelpDesk Support

    Highly skilled 24/7 HelpDesk Support

    icon

    Product Support

    Boost your product support with our expertise.

    MANAGED SERVICES

    icon

    Server Management

    Don’t let server issues slow you down. Let us manage them for you.

    icon

    Server Monitoring

    Safeguard your server health with our comprehensive monitoring solutions.

    STAFF AUGMENTATION

    icon

    Hire an Admin

    Transform your business operations with our expert administrative support.

    icon

    Hire a Team

    Augment your workforce with highly skilled professionals from our diverse talent pool.

  • CloudOps
    Case Study

    How we helped a Private Deemed University in India, save US $3500/m on hosting charges!

    READ CASESTUDY
    icon

    AWS Well Architected Review

    Round-the-clock for uninterrupted efficiency

    icon

    Optimize

    Efficient CloudOps mastery for seamless cloud management

    icon

    Manage

    Automated CI/CD pipeline for seamless deployments

    icon

    Migrate

    Upgrade the journey, Migrate & Modernize seamlessly

    icon

    Modernize

    Simplify compliance complexities with our dedicated services

    icon

    FinOps as a Service

    FinOps as a Service

  • SecOps
    Case Study

    How we built a scalable Odoo solution for TravelTech service provider!

    READ CASESTUDY
    icon

    VAPT

    Vulnerability Assessment and Penetration Testing

    icon

    Source Code Review

    Ensuring source code security ans safe practices to reduce risks

    icon

    Security Consultation

    On demand services for improving server security

    icon

    System Hardening

    Reduced vulnerability and proactive protection

    icon

    Managed SoC

    Monitors and maintains system security. Quick response on incidents.

    icon

    Compliance as a Service

    Regulatory compliance, reduced risk

  • Insights
    Case Study

    How we helped a Private Deemed University in India, save US $3,500/m on hosting charges!

    READ CASESTUDY
    icon

    Blog

    Explore our latest articles and insights

    icon

    Case Studies

    Read about our client success stories

    icon

    Flipbook

    Explore our latest Flipbook

    icon

    Events

    Join us at upcoming events and conferences

    icon

    Webinars

    Watch our educational webinar series

  • Our Story
  • Contact Us

Interested to collaborate?

Get in touch with us!

Ready to elevate your business with certified cloud expertise? Contact us today to learn how our team can help you leverage cloud technology to drive growth, streamline operations, and enhance security.

  • AWSAWS
  • Azure CloudAzure Cloud
  • Google CloudGoogle Cloud
  • Akamai CloudAkamai Cloud
  • OVHOVH
  • Digital OceanDigital Ocean
  • HetznerHetzner
  • Kubernetes Consultancy Services
  • K8s & Cloud native Solutions
  • 24/7 Infrastructure Monitoring
  • DevOps as a Service
  • Cloud CI/CD Solutions
  • White Labeled MSP Support
  • Our story
  • Life@SupportSages
  • Insights
  • Careers
  • Events
  • Contact Us

Connect with us!


LinkedInFacebookXInstagramYouTube

aws partneraws advanced partner
SupportSages

Copyright © 2008 – 2026 SupportSages Pvt Ltd. All Rights Reserved.
Privacy PolicyLegal TermsData ProtectionCookie Policy
PHP script to Change MySQL Table Prefix

PHP script to Change MySQL Table Prefix

Afsal

  • 3 min read
PHP script to Change MySQL Table Prefix

Generating audio, please wait...

When installing applications like Magento, WordPress, etc, there will be an option to specify a MySQL table prefix which is to be used with the application’s MySQL database. This prefix will be added to all the tables in the application’s database. Adding such a prefix increases the security of the application as it is a safeguard against attacks like SQL injection.

If you need to change the table prefix after installation or if you need to add a table prefix if you haven’t done so while installing, you can do it by manually changing the name of all the tables via phpMyAdmin or via SSH. As this method is time consuming considering most applications has a lot of tables, we can do this easily with the help of a PHP script.

We will be using PHP MySQLi in our script to perform the renaming operation.

We can use the following script to read the database attributes and then change the table prefix.


<?php
if(isset($_POST['submit'])){
$database_host = $_POST['dbhname'];
$database_user = $_POST['dbuname'];
$database_password = $_POST['password'];
$database_name = $_POST['dbname'];
$new_table_prefix = $_POST['newpref'];
$old_table_prefix = $_POST['oldpref'];


// Connect to database
$db = mysqli_connect($database_host, $database_user, $database_password, $database_name) or die('MySQL connect failed');

// List all tables
$result = mysqli_query($db, "SHOW TABLES") or die('SHOW TABLES failed');

// Loop through all tables
while($row = mysqli_fetch_array($result)) {
    $old_table = $row[0];

    // Check if old prefix is correct
    if(!empty($old_table_prefix) && !preg_match('/^'.$old_table_prefix.'/', $old_table)) {
        echo "Table $old_table does not match prefix $old_table_prefix<br/>\n";
        continue;
    }

    // Preliminary check: Is the old table prefix the same as the new one?
    if(preg_match('/^'.$new_table_prefix.'/', $old_table)) {
        echo "Table $old_table already done<br/>\n";
        continue;
    }

    // Construct the new table prefix and rename the table
    if(!empty($old_table_prefix)) {
        $new_table = preg_replace('/^'.$old_table_prefix.'/', $new_table_prefix, $old_table);
    } else {
        $new_table = $new_table_prefix.$old_table;
    }
    // Rename the table
    echo "Renaming $old_table to $new_table<br/>\n";
    mysqli_query($db, "RENAME TABLE `$old_table`  TO `$new_table`");
    header("Refresh: 2; URL = prefix.php"); //Name of the script file
}
echo "Renaming complete";
mysqli_close($db);
}else{
?>
<html>
<head>
<title>Change Table Prefix</title>
<body>

<form name="PrefixChanger" method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<table>
<tr><td>DB Hostname</td><td> <input type="text" required name="dbhname" value=""></td></tr>
<tr><td>DB Name</td><td> <input type="text" required name="dbname" value=""></td></tr>
<tr><td>DB Username</td><td> <input type="text" required name="dbuname" value=""></td></tr>
<tr><td>DB Password</td><td> <input type="password" required name="password" value=""></td></tr>
<tr><td>New Prefix</td><td> <input type="text" required name="newpref" value=""></td></tr>
<tr><td>Old Prefix</td><td> <input type="text" name="oldpref" value=""></td></tr>
<tr><td></td><td align="right"><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>

</body>
</head>
</html>
<?php } ?>
Upload the above PHP script to the website and call it using a web browser. It will display the following output.

MySQL

The current database structure looks like this.

MySQL

To change the prefix of the tables, fill the form appropriately and click submit.

MySQL

If the entered values are correct, we will see prefix of each table being changed.

MySQL

Check the database to verify the changes.

MySQL

Get 24/7 expert server management

  • Howtos
  • Linux
  • magento
  • MySQL
  • WordPress

How to Redirect Visitors based on IP Location

How to Redirect Visitors based on IP Location
  • Linux
logo

Introduction to Magento2 Command Line Interface (CLI)

Introduction to Magento2 Command Line Interface (CLI)
  • Linux
  • magento
logo

Magento2: Configuration File

Magento2: Configuration File
  • Linux
  • magento
logo

Steps to enable TLSv1.2 for PayPal /Stripe connections

Steps to enable TLSv1.2 for PayPal /Stripe connections
  • Howtos
  • Linux
  • Troubleshooting
logo

Posts by Afsal

Afsal is a tech enthusiast who is always ready to learn new technologies and explore new territories. Although his current focus revolves around the System Administration world, his never ending passion for technologies always keeps him active in other areas as well. He is currently working as a Systems Engineer at SupportSages.