Last active
July 23, 2021 09:41
Mass Find and Replace MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* MySQL Mass Find and Replace | |
* Author: Lee Woodman - https://www.linkedin.com/in/leewoodman | |
* License: GNU | |
*/ | |
// Connect to your MySQL database. | |
$hostname = "localhost"; | |
$username = "root"; | |
$password = "password"; | |
$database = "db_name"; | |
mysql_connect($hostname, $username, $password); | |
// The find and replace strings. | |
$find = "what_i_need_to_find"; | |
$replace = "what_i_need_to_replace_with"; | |
// Test mode | |
$test_mode = false; | |
// Loop through all tables and columns | |
$loop = mysql_query(" | |
SELECT | |
concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''{$find}'', ''{$replace}'');') AS s | |
FROM | |
information_schema.columns | |
WHERE | |
table_schema = '{$database}'") | |
or die ('Cannot loop through database fields: ' . mysql_error()); | |
while ($query = mysql_fetch_assoc($loop)) | |
{ | |
if ($test_mode) | |
echo "{$query['s']}<br/>"; | |
else | |
mysql_query($query['s']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment