<?PHP
// configuration settings
// change this to the mysql server host
DEFINE('MYSQL_SERVER_HOST', 'localhost');
// change this to the correct username
DEFINE('MYSQL_SERVER_USERNAME', 'my_username');
// change this to the correct password
DEFINE('MYSQL_SERVER_PASSWORD', 'my_password');
// change this to the correct database
DEFINE('MYSQL_DATABASE', 'my_db');
DEFINE('OLD_TEXT', 'pirates');
DEFINE('NEW_TEXT', 'ninjas');
// connect to the server and database
$con = mysql_connect (MYSQL_SERVER_HOST, MYSQL_SERVER_USERNAME, MYSQL_SERVER_PASSWORD) or DIE(SPRINTF ('Error (%d): %s', mysql_errno(), mysql_error()));
mysql_select_db (MYSQL_DATABASE, $con) or DIE(SPRINTF('Error (%d): %s', mysql_errno(), mysql_error()));
// this is the mysql query that will do the text replacement
$sql = SPRINTF ('UPDATE `%%s` SET %%s=REPLACE(%%s, %s, %s)', OLD_TEXT, NEW_TEXT);
// next get all database information
$r1 = mysql_query(SPRINTF('SHOW TABLES FROM `%s`', MYSQL_DATABASE)) or DIE(SPRINTF('Error (%d): %s', mysql_errno(), mysql_error()));
WHILE ($d1 = mysql_fetch_row ($r1)){
$r2 = mysql_query(SPRINTF('SHOW COLUMNS FROM `%s`', $d1[0])) or DIE(SPRINTF('Error (%d): %s', mysql_errno(), mysql_error()));
WHILE ($d2 = mysql_fetch_assoc($r2)){
$r3 = mysql_query(SPRINTF($sql, $d1[0], $d2['Field'], $d2['Field'])) or DIE(SPRINTF('Error (%d): %s', mysql_errno(), mysql_error()));
}
}