1 Replies - 1023 Views - Last Post: 28 April 2012 - 05:48 PM Rate Topic: -----

#1 legithugo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-July 11

Mysql Search & Replace all tables

Posted 28 April 2012 - 05:25 PM

Hello everyone, I currently need to know how to place a query where it will search every column and table in a MYSQL database for a entered string, and replace it with another string. Though PHP that is.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Mysql Search & Replace all tables

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Mysql Search & Replace all tables

Posted 28 April 2012 - 05:48 PM

You'll need to query the INFORMATION_SCHEMA to get a list of table/column pairs, then loop through that one row at a time to generate and execute an UPDATE query for each pair. Pseudocode:
$columns = GetAllColumnsFromInfoSchema();

foreach($columns as $column)
{
  $tableName = $column['TableName'];
  $columnName = $column['ColumnName'];
  $query = "UPDATE " . $tableName . " SET " . $columnName . " = 'NewValue' WHERE " . $columnName . " = 'OldValue'";
  ExecuteQuery($query);
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1