Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 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 Bookmark and Share

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


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 257 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?