utf8 and mysql... need help!
Posted: Sat Sep 13, 2008 9:39 am
im trying to add unicode support for my news system... but i have problems when printing the page...
I tried many things... htmlentities, utf8_encode/decode, etc. but none worked well...
I just tried a little example to see if i could just print unicode in a normal encoded utf8 page:
it worked so i thought it could work with my news system too, but it doesn't.
In my DB table encoding is utf8_general_ci and things are saved like the would have to... i used htmlspecialchars for this...
even in my adminnews.php they are shown correctly but not in my news.php...
Like this: htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
An example is:
????? &á<>/äwdé
in DB is
????? &á<>/äwdé
Everything ok right now...
but when i try to retrieve it with my news.php (headers doctype xml encoding and all is set right) and echo it, it shows like this:
????? &?<>/?wd?;
i tried using utf8_decode since i thought maybe DB utf8 encoding was the problem but i didn't as expected neither...
news.php:
adminnews.php
Maybe im blind and can't see my mistake... so pls take a look and see if u can find my mistake 
I really need help with this...
Thx in advance!
I tried many things... htmlentities, utf8_encode/decode, etc. but none worked well...
I just tried a little example to see if i could just print unicode in a normal encoded utf8 page:
Code: Select all
<?php echo '<' . '?xml version="1.0" encoding="utf-8"?' . '>' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
if (isset($_POST['submitted'])) {
$msg = $_POST['message'];
$msge = htmlspecialchars($msg, ENT_QUOTES, 'UTF-8');
echo $msge;
}
?>
<form action="<?php $PHP_SELF ?>" method="post" accept-charset="UTF-8">
<textarea rows="8" cols="55" name="message"><?php if(isset($_POST['message'])) echo $_POST['message']; ?></textarea></p>
<p><input type="submit" name="submit" value="Añadir" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
</body>
</html>In my DB table encoding is utf8_general_ci and things are saved like the would have to... i used htmlspecialchars for this...
even in my adminnews.php they are shown correctly but not in my news.php...
Like this: htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
An example is:
????? &á<>/äwdé
in DB is
????? &á<>/äwdé
Everything ok right now...
but when i try to retrieve it with my news.php (headers doctype xml encoding and all is set right) and echo it, it shows like this:
????? &?<>/?wd?;
i tried using utf8_decode since i thought maybe DB utf8 encoding was the problem but i didn't as expected neither...
news.php:
Code: Select all
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
<script type="text/javascript">
function openComments(url)
{
comments = window.open(url, "Comentarios", "menubar=0,resizable=0,width=380,height=480")
comments.focus()
}
</script>
</head>
<body>
<?php
include ('connect.php');
$query = "SELECT id, title, author, editor, post, DATE_FORMAT(edate,' %d/%m/%Y, %H:%i:%s') as edd, DATE_FORMAT(date,' %d/%m/%Y, %H:%i:%s') as sd FROM news_posts ORDER BY DATE DESC";
$result = @mysql_query($query);
if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$url = 'comentarios.php?id='.$row['id'];
$tit = $row['title'];
$na = $row['author'];
$edt = $row['editor'];
$po = nl2br($row['post']);
echo '<h3>'.$tit.'</h3>Fecha: '.$row['sd'].'<br />Por: <b>'.$na.'</b><br /><br />'.$po.'<br /><br />';
if (!empty($edt)){
echo "<p style='font-family:Georgia; font-size:10;'><i>Última edición por <b>".$edt."</b> el ".$row['edd']."</i></p><br />";
}
echo '<a href="javascript:openComments(\''.$url.'\')">Comentarios</a><hr />';
}
} else {
echo 'No hay noticias para mostrar.';
}
?>
</body>
</html>Code: Select all
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
</head>
<body>
<?php
include('connect.php');
include("sesiones_foro.php");
If($user->data['is_registered']){
$group = $user->data['group_id'];
$nam = $user->data['username'];
If ($group!="5" && $group!="7"){
echo "No estás autorizado para entrar en esta página.";
die;
}
}Else{
echo "No estás autorizado para entrar en esta página.";
die;
}
$query = "SELECT DATE, id, title FROM news_posts ORDER BY DATE DESC";
$result = @mysql_query($query);
if ($result) {
echo '<div align="center">
<a href="anadirnoticias.php">Añadir una noticia nueva.</a><br />
<a href="http://www.otakunosekai.es/index.php">Inicio.</a><br /><br />
<table border="1">
<tr>
<td><b>Fecha</b></td>
<td><b>ID</b></td>
<td><b>Título</b></td>
<td><b>Borrar</b></td>
<td><b>Editar</b></td>
</tr>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td>'.$row['DATE'].'</td>
<td>'.$row['id'].'</td>
<td>'.$row['title'].'</td>
<td><a href="borrarnoticias.php?id='.$row['id'].'">Borrar</a></td>
<td><a href="editarnoticias.php?id='.$row['id'].'">Editar
</tr>';
}
} else {
echo 'No hay noticias para mostrar.';
}
?>
</body>
</html>I really need help with this...
Thx in advance!