<?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()));
}
}