Replace a word in MySql database
Hi Is there a way to write a sql statement to search and replace a word in a mySql database? For example Find "word1" in "testDatabase" and replace i开发者_如何学编程t with "word2".
$tables = table_list;
foreach($tables as $t)
mysql_query("Update $t SET wordcol = 'word2' WHERE wordcol = 'word1'
If it's more complicated than that you should look at not changing the sql and simply altering the phrasing in the php/html when that word gets output.
You could even do something like:
function my_sql_fetch($query){
$fetch = mysql_query($query);
$return = array();
while($rec = mysql_fetch_assoc($fetch)){
foreach($rec as &$val)
str_replace('word1', 'word2', $val);
$return[] = $rec;
}
return $return;
}
You can use an update statement like this
update colname = 'word2' where colname = 'word1'
精彩评论