error using php to print out special charcters from a database
Alright, what I'm trying to do is print out a few lines of text onto a page from a database, and the problem I'm running into is when it sees something like a '
in the text, it puts out a ?
. So what I'm wondering is there a way around this? I know with HTML you can use special characters, but I'm working with dynamic data here. I did try doing this, but it's just not working:
<?php
include("connect.php");
$queryCurUser = "SELECT * FROM wn_current;";
$queryResult2 = mysql_query($queryCurUser, $conn) or
die ("queryResult Failed: ". mysql_error());
while 开发者_StackOverflow($row = mysql_fetch_array($queryResult2))
{
$body = $row['body'];
echo $row['heading'];
echo"<br/>";
if($body == "'"){
echo"hot dog";
}
echo"<br/>";
echo $row['pdflink'];
}
?>
You need to change the character set used in the database to one with more characters - UTF-8, for instance.
You need to tell the browser what encoding the data is.
Most likely this tag will solve your woes:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If that doesn't work, try this one:
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
Check the character set that your database table uses; you may want to change it to use UTF-8. Also consider using htmlentities() on your echoed output with the correct charset to convert any characters that have HTML character entity equivalents, e.g.
echo htmlentities($body, ENT_NOQUOTES, 'UTF-8', false);
If you store international characters (á, ê, etc..), not only do you need to choose the correct character encoding for your database (e.g. UTF-8), but you also need to tell the browser to use the correct character set, by way of
a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
or a php header:
header("Content-Type: text/html; charset=utf-8");
Also, it may be necessary to specify the charset that the MySQL extension should use with mysql_set_charset().
$body=str_replace['"', """, $body]; // double quotes
seemed to work
精彩评论