Jump to content

Search Replace text in whole mysql database, is it possible ?


ukscotth

Recommended Posts

Hi,

 

I run a social networking site and I want to be able to change my members usernames when neccessary.  The problem is there usernames are stored in various different tables under various different field names.  Is there a way to run a query that searches and replaces text in the whole database ?

 

I know you can do it by backing up the database and the using search replace in notepad but I wanted to add the option to my admin panel.

 

Any ideas ?

 

Thanks in advance.

 

Scott.

 

 

 

Link to comment
Share on other sites

I don't know if there is a specific way to do that (would be useful if there was) but for your problem have you considered using ID numbers, and having a table with usernames to ID numbers, that way they keep the ID number for life and only one table needs updated everytime they change their username? It would mean selecting the table with names to ID everytime you wanted to print their name but it'd be easier to change it then.

Link to comment
Share on other sites

Hey Scott,

 

Unfortunately I don't think you're going to like my reply (reply and not an answer). Basically you need to consider a relational database structure. If you have a primary key in your main user table, you should be using it to reference your user in other tables rather than their usernames.

 

Your primary key will (should) never change, so you can easily update a users details. once it's updated it's referenced by an id so is easy to change at anytime.

 

Hope that was at least helpful if not the answer.

 

As far as your question goes run a query for each table

 

UPDATE `the_table` SET `the_field`='new_username' WHERE `the_field`='old_username'

Link to comment
Share on other sites

As far as your question goes run a query for each table

 

UPDATE `the_table` SET `the_field`='new_username' WHERE `the_field`='old_username'

UPDATE `the_table` SET `the_field`='new_username' WHERE `the_field`='old_username'

UPDATE `the_table` SET `the_field`='new_username' WHERE `the_field`='old_username'

 

Just run one script that updates every table

Link to comment
Share on other sites

Loop function to do what you want:

<?php

// Put the names of all the tables and their corrosponding username fields in this array.
$table = Array(
     "table_name" => "field_name",
     "table_name2" => "field_name2"
);

Foreach($table As $Tablename=>$Fieldname){
     $Query = "UPDATE `".$Tablename."` SET `".$Fieldname."`='".$new_username."' WHERE `".$Fieldname."`='".$old_username."'";
     
     $result = mysql_query($Query) or die("Could not update username in table: ".$Tablename."<br />".mysql_error();
}

?>

 

If you want to change usernames that are embedded inside other text, you will need to get that data first with a select query, replace the text, THEN update.

 

something like:

 

<?php

// Put the names of all the tables and their corrosponding username fields in this array.
$table = Array(
     "table_name" => "field_name",
     "table_name2" => "field_name2"
);

Foreach($table As $Tablename=>$Fieldname){
     
$Query = "SELECT `".$Fieldname."` FROM `".$Tablename."`  WHERE `".$Fieldname."` LIKE '%".$old_username."%'";

$result = mysql_query($Query) or die("Could not update username in table: ".$Tablename."<br />".mysql_error();
$array = mysql_fetch_array($result,MYSQL_ASSOC);

// If its not a multi-dimensional array, make it one.
if(!isset($array[0]) || $array[0] == null){
	$array[0] = $array;
}


Foreach($array As $row){
	$oldData = $row[$Fieldname];
	$newData = str_replace($old_username,$new_username,$oldData);

	$Query = "UPDATE `".$Tablename."` SET `".$Fieldname."`='".$new_username."' WHERE `".$Fieldname."`='".$oldData."'";
	$result = mysql_query($Query) or die("Could not update username in table: ".$Tablename."<br />".mysql_error();
}
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.