Replace word in mysql database
I found this script and I think is useful. Now I update the word „test“ in my database with word „test2“ . I need this bacuse of changing the url of my cdn and without this script it will be hours to do this :biggrin: This is the script:
<?php
// edit this line to add old and new terms which you want to be replaced
$search_replace = array( 'oldword' => 'newword', 'oldword2' => 'newword2' );
//change the localhost,username,password and database-name according to your db
mysql_connect("localhost", "user", "userpass") or die(mysql_error());
mysql_select_db("dbname") or die(mysql_error());
$show_tables = mysql_query( 'SHOW TABLES' );
while( $st_rows = mysql_fetch_row( $show_tables ) ) {
foreach( $st_rows as $cur_table ) {
$show_columns = mysql_query( 'SHOW COLUMNS FROM ' . $cur_table );
while( $cc_row = mysql_fetch_assoc( $show_columns ) ) {
$column = $cc_row['Field'];
$type = $cc_row['Type'];
if( strpos( $type, 'char' ) !== false || strpos( $type, 'text' ) !== false ) {
foreach( $search_replace as $old_string => $new_string ) {
$replace_query = 'UPDATE ' . $cur_table .
' SET ' . $column . ' = REPLACE(' . $column .
', \'' . $old_string . '\', \'' . $new_string . '\')';
mysql_query( $replace_query );
}
}
}
}
}
echo 'replaced';
mysql_free_result( $show_columns );
mysql_free_result( $show_tables );
mysql_close( $mysql_link );
?>
Or download from here. Rename .txt to .php and run it : php search_replace.php , thats it!
Вчера аз големия акъл реших да продължа мисия „внедряване на cdn в блога“ като не мислих че нещата ще се объркат толкова 🙂 Няма да изпадам в подробности , само ще споделя че усрах базата и се оказа че последния бекъп е от 29.12.2012 . Всички бекъпи на бекъп сървъра са на другия mysql сървър, а блога нали работи на localhost … защо да има бекъп и той 🙂 Е вече има. Загубата не е кой знае каква , снимки и тн всичко се пази. Днес започвам новата мисия „възтановяване на сайта“ Ще се опитам да извлека всичко от sql-а който имам за 24.03.2012 . Това което разбрах е че backup-а който прави wordpress-a за един … не става и че ще си я карам с bash скрипта ръчно с кронтаб. Надявам се до няколко дни всичко да си бъде по старо му , а вчера за малко да спра блога завинаги. Проблема е , че той работи от много години , че в него има много интересни случки и мисли които съм писал. Всяко зло за добро:) Айде до следващия пост!