Blogvaria

This page is brought to you by Blogvaria (http://blog.evaria.com).

To obtain more information, ask questions and interact please visit our website.

Back to Blogvaria landing page
Feedback
Subscribe
   
Blogvaria

 

The personal pages

MySQL; Update all rows in a second

TrackBack | Filed by Thomas under Programming | Post popularity 7%

I’ve just converted an Excel sheet to MySQL to import numerous data for a new client project. Afterwards when I verified the data all the “Room” numbers was written with an extra .0 at the end besides a few other smaller issues.

So how do you clean up a few hundred rows quickly? You simply write a tiny PHP script to do the job for you:

$query = "SELECT * FROM Table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
while($row = mysql_fetch_assoc($result))
{
    # Assuming you have an auto_increment Field named ID
    $ID = $row['ID'];
    # Replace the unwanted text with new text
    $updateRow = str_replace("before","after",$row['FieldName']);
    # Update your table with the new value
    $UpdateRecords = "UPDATE Table SET FieldName='$updateRow' WHERE ID='$ID'";
    $Update = mysql_query($UpdateRecords) or die("Query failed: " . mysql_error());
}

Could prove useful to you as well someday - it sure did for me :)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • BlinkList
  • blogmarks
  • del.icio.us
  • De.lirio.us
  • digg
  • Furl
  • NewsVine
  • Netscape
  • Reddit
  • Spurl
  • SphereIt
  • Technorati
  • YahooMyWeb
  • DZone
  • feedmelinks
  • Linkter
  • Ma.gnolia
  • Slashdot
  • StumbleUpon
  • TailRank
  • co.mments

No comments yet.

Leave a Comment

Akismet has protected Blogvaria from 68,506 spam comments. Design by Evaria.com. Powered by WordPress.
Our beloved and trusted server has rendered 1.203 pages so far today, an amazing 3.891 pages yesterday
and even more astonishingly 104.339 pages since 13 August 2008 alone without dropping a byte nor a pixel.

Close
E-mail It