Web Development

Replacing Text in a MySQL Database Using PHP

Replacing Text In A MySQL Database Using PHP

You may have the word Ben in a lot of your MySQL rows and want to repalce that word with a new word such as Jim.

If you have 10+ of rows with the name Ben in it, it could take a while having to modify each table manually…

Here is a quick and easy way to do it all at once with some simple PHP and MySQL:

<?php
// Connect to your MySQL Database here etc...

// Now with the MySQL Query
mysql_query("update table_name set text=REPLACE(text,'Ben','Jim')");

// Rest of your PHP code...
?>

Replace table_name and text with your own database fields and you’re all set!

Pretty simple huh?

Enjoy! :)

About the author

Written by Ben Sinclair.

http://www.webmaster-resources101.com Ben Sinclair is the Webmaster of Webmaster Resources 101 (http://www.webmaster-resources101.com/) and Webmaster Forums 101 (http://www.webmaster-forums101.com/)

If you found this post useful you may also want to check these out:

  1. Adding records to a MySQL database using PHP
  2. How To Backup Your MySQL Database With PHP
  3. Publishing Newsletters Using PHP & MySQL
  4. Showing MySQL Results in Pages
  5. Setting Up Apache, PHP & MySQL On Windows
  6. Publishing Newsletters Using PHP & MySQL – 3