I use the code below to replace these funky characters when they end up in the database. In phpmyadmin the queries work just fine, however, when I use something similar in php, they don't throw any errors and they also don't work.
In mysql:
In php:
What am I doing wrong here?
In mysql:
Code:
UPDATE news SET title=(replace (title, '’','\'')); UPDATE news SET description=(replace (description, '’','\'')); UPDATE news SET title=(replace (title, '‘','\'')); UPDATE news SET description=(replace (description, '‘','\'')); UPDATE news SET title=(replace (title, ''','\'')); UPDATE news SET description=(replace (description, ''','\'')); UPDATE news SET title=(replace (title, '&','\&')); UPDATE news SET description=(replace (description, '&','\&')); UPDATE news SET title=(replace (title, '–','-')); UPDATE news SET description=(replace (description, '–','-'));
Code:
$find[] = '“'; // left side double smart quote
$find[] = 'â€'; // right side double smart quote
$find[] = '‘'; // left side single smart quote
$find[] = '’'; // right side single smart quote
$find[] = '…'; // elipsis
$find[] = '—'; // em dash
$find[] = '–'; // en dash
$replace[] = '"';
$replace[] = '"';
$replace[] = "'";
$replace[] = "'";
$replace[] = "...";
$replace[] = "-";
$replace[] = "-";
$cleanQuery = "SELECT * FROM news ORDER BY id DESC LIMIT 50";
$runSelect = mysql_query($cleanQuery) or exit(mysql_error().$cleanQuery);
while($tickerResults = mysql_fetch_assoc($runSelect)) {
$saveWeather = "UPDATE news SET title = '".mysql_real_escape_string(str_replace($find, $replace, $tickerResults['title']))."', description = '".mysql_real_escape_string(str_replace($find, $replace, $tickerResults['description']))."' WHERE id = '".$tickerResults['id']."'";
//exit($saveWeather);
$runInsert = mysql_query($saveWeather) or exit(mysql_error().$saveWeather);
}
Comment