Web Hosting Talk







View Full Version : Find and Replace in MySQl database with PHPMyAdmin?


[Axelsson]
02-29-2008, 06:57 PM
I have a database filled with different URLs.
Like this:
http://www.sponsor.com/product345/
http://www.sponsor.com/product123/
http://www.sponsor.com/product647/
http://www.sponsor.com/product765/

I would like to replace
http://www.sponsor.com/ with http://www.mydomain.com/

But let the product-pages be left untouched so the new URLs will look like:
http://www.mydomain.com/product345/
http://www.mydomain.com/product123/
http://www.mydomain.com/product647/
http://www.mydomain.com/product765/

Does anyone know what the SQL-Query would look like?

case
03-01-2008, 01:44 AM
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find’, ‘replace’);

Make sure you backup before trying...

You can also use mysqldump to dump the table out to a SQL file, find and replace the string in a text editor, drop table, and pipe the SQL file back into mysql

mysqldump -u user -h host -ppasswd database table > table.sql && sed 's/find /replace/g' table.sql > tmp_table.sql && mysql -u user -h host -p < tmp_table.sql

case
03-01-2008, 02:05 AM
Make sure you add a drop table which I forgot to add if you use the second method!

mysqldump -u user -h host -ppasswd database table --add-drop-table > table.sql && sed 's/find /replace/g' table.sql > tmp_table.sql && mysql -u user -h host -p < tmp_table.sql

[Axelsson]
03-01-2008, 07:39 AM
Thanks case! :)