Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Looping through Mysql to Update fields too slow

11:38 am on Oct 2, 2009 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 6, 2008
votes: 0


I'm trying to update a mysql table so that the url field have a nicely formatted URL. This method works but it is very slow, I have to set a big time limit so it completes. If I start using it on larger tables I imagine it will takes ages, the current table has about 3k records. Is there a faster way of doing this?


$sql = "SELECT * FROM table";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result)){
$title = $row['title'];
$id = $row['id'];

$url = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);
$url = strtolower(str_replace(" ","",$url));

$sql="UPDATE `table` SET `url`='$url' WHERE `id`='$id' LIMIT 1";