Quick Search:
 
 PHP Code: Search and Replace On Every Field In Every Table Jump to:  
Category: >> PHP Code >> Search and Replace On Every Field In Every Table  

<< lastnext >>

Snippet Name: Search and Replace On Every Field In Every Table

Description: This script will do a global search and replace on every field in every table in a given database. It gets the tables, then loops across them getting the columns, doing a search and replace.

Also see:
» Make alternating color table rows auto...
» Search and Replace On Every Field In E...
» Export mySQL results to CSV
» Convert UK Dates To mySQL Format Dates
» Search array elements for a substring
» Toggle a column bit regardless of state
» Search All Words Of A String In mySQL
» Highlight table rows on rollover
» Converting Rows to Columns
» Output mySQL data in columns
» Dynamically Add/Remove rows in HTML ta...
» Regular Expressions: REGEXP_REPLACE
» LOCKS: Table Locking
» TABLESPACE: Show contiguous space
» TABLESPACE: SYSAUX tablespace
» TABLESPACE: Tablespace management
» TABLESPACE: List tablespaces, files, ...
» TABLESPACE: Dropping Tablespaces
» TABLESPACE: Alter Permanent Tablespace
» TABLESPACE: Transportable tablespaces
» TABLESPACE: Tempfile operations
» TABLESPACE: Create temp tablespace
» TABLESPACE: Change UNDO tablespace
» TABLESPACE: Undo Tablespace
» TABLESPACE: SYSAUX Tablespace
» TABLESPACE: Set default tablespace type
» TABLESPACE: Oracle Managed Auto-extend...
» TABLESPACE: Permanent Tablespace Using...
» TABLE: Constraints
» TABLE: Options

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: May 28th, 2010

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


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org