Replace URL in the PhpMyAdmin Database Using MySQL Scripts

Here’s how to replace URLs in the PhpMyAdmin database.

If you’re migrating your WordPress website to a new domain or duplicating your site for development purposes, this is a handy way way to replace the URL using MySQL scripts.

If you have access to /wp-admin then I’d recommend you use Search and Replace plugin. If you don’t have an easy access to /wp-admin then you can use this code.

Step 1: Find your PhpMyAdmin database from your host.
Step 2: Select the database for your WordPress site.

Step 3: Select SQL tab from the tool bar and type the following code.
Replace ‘Existing URL’ and ‘NEW URL’ with your URLs. Retain the quotation marks.

UPDATE wp_options SET option_value = replace(option_value, 'Existing URL', 'New URL') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, 'Existing URL', 'New URL');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'Existing URL','New URL');
UPDATE wp_usermeta SET meta_value = replace(meta_value, 'Existing URL','New URL');
UPDATE wp_links SET link_url = replace(link_url, 'Existing URL','New URL');
UPDATE wp_comments SET comment_content = replace(comment_content , 'Existing URL','New URL');
UPDATE wp_posts SET post_content = replace(post_content, 'Existing URL', 'New URL');
UPDATE wp_posts SET guid = replace(guid, 'Existing URL','New URL');

Press “Simulate Query” to see how many changes will be made. If it returns “0” it means you must have made an error somewhere because “0” means nothing will be changed. Press “Go” to make the changes.

The command above will look for the existing URL in the specified field within the specified table, and replace with the new URL.

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find string’, ‘replace string’);

Not sure which table to field to look into? Simply use the code above because the field and table names above are the usual places where the URL is found, including the image URLs.

But once you have access to the WordPress admin area after using the code above, go ahead and use the Find and Replace plugin to look for further areas where your old URL need to be replaced.