punkstar69 Posted June 20, 2010 Share Posted June 20, 2010 Hello guys! Im new in here, so welcome me! heh! I was trying to find some help about a php script and i found your forum. I would appreciate if someone could help me to this.. I run a php script with SQL Update command that does a replace to some strings i give it. The problem is i want to replace about 8 words, but my hosting provider says to me that i cant run 8 cron jobs seperately, so im just wondering how could i make a Multiple String Replacer php script.. I ill show you my php code and i hope it will help you advising me what to do. <?php mysql_connect("localhost","username","password"); mysql_select_db("dbname"); $old_str = 'Mike'; $new_str = 'George'; $dbname = 'dbname'; // connect to mysql $con = mysql_connect("localhost","username","password") or die('no connection:' . mysql_error()); $db = mysql_select_db("dbname") or die ('cant select db: ' . mysql_error()); $num_changed = $num_changed_ci = $total_changed = 0; // retreive each table $sql = "SHOW TABLES FROM `$dbname`"; $result = mysql_query($sql) or die ('could not get tables: ' . mysql_error()); // reteive each column in each table. while ($tbl= mysql_fetch_row($result)) { echo "\nTable: {$tbl[0]} <br />"; $sql = "SHOW COLUMNS FROM `{$tbl[0]}`"; $res = mysql_query($sql) or die ('could not get columns: ' . mysql_error()); while ($col = mysql_fetch_array($res)) { // determine the correct update statement based on the user's preference $sql = "UPDATE `{$tbl[0]}` SET `{$col[0]}` = REPLACE(`{$col[0]}`, '{$old_str}' , '{$new_str}')"; $r = mysql_query($sql) or die ('could not update the field: ' . mysql_error()); if($r) { echo "\n\t <b>{$col[0]}</b>:"; $num_changed = mysql_affected_rows() + $num_changed_ci; if($num_changed) { echo "<font color='blue'> " . $num_changed . " values affected</font>"; $total_changed += $num_changed; } else { echo " no change"; } echo "<br />"; } else { echo "\n\t <b>{$col[0]}</b>: <font color='red'>FAILED</font><br />"; } } } @mysql_close($con); echo "<h3>$total_changed fields were affected.</h3>"; ?> So this is it.. I got 7 more names to change to i made 1.php, 2.php etc This one has $old_str = 'Mike'; $new_str = 'George'; The other one has $old_str = 'John'; $new_str = 'Jim'; etc.. I tried to add $old_str1,2 etc and then add a second line with those vars under the: $sql = "UPDATE `{$tbl[0]}` SET `{$col[0]}` = REPLACE(`{$col[0]}`, '{$old_str}' , '{$new_str}')"; but i had no luck.. Only the one was working. Any Ideas of making such a thing? Quote Link to comment https://forums.phpfreaks.com/topic/205337-using-update-for-multiple-strings/ Share on other sites More sharing options...
fenway Posted June 20, 2010 Share Posted June 20, 2010 Define "working". Quote Link to comment https://forums.phpfreaks.com/topic/205337-using-update-for-multiple-strings/#findComment-1074680 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.