utf8 and mysql... need help!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

utf8 and mysql... need help!

Post by BETA »

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:

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&ntilde;adir" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
</body>
</html>
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:

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>&Uacute;ltima edici&oacute;n por <b>".$edt."</b> el ".$row['edd']."</i></p><br />";
    }
echo '<a href="javascript&#058;openComments(\''.$url.'\')">Comentarios</a><hr />';
}
} else {
echo 'No hay noticias para mostrar.';
}
?>
</body>
</html>
adminnews.php

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&aacute;s autorizado para entrar en esta p&aacute;gina.";
            die;
}
}Else{
echo "No est&aacute;s autorizado para entrar en esta p&aacute;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&ntilde;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&iacute;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>
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! :|
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: utf8 and mysql... need help!

Post by BETA »

Sorry for double post guys but i still need help on this one :(
any ideas? didn't i explain it clearly?
Thx in advance!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: utf8 and mysql... need help!

Post by Weirdan »

In your 'connect.php' add

Code: Select all

 
mysql_query('set names utf8');
 
after connect code. This will ensure the connection character set is set to utf8. Otherwise (by default) mysql converts data it sends to your application to latin1.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: utf8 and mysql... need help!

Post by BETA »

Well i have an option that was in latin1 set to utf8 which i think is the same... anyway ill check this out.
Problem is it DOES show up normally in adminnews.php :? and i don't know why it doesn0t in news.php.
BETA
Forum Commoner
Posts: 47
Joined: Fri Jul 25, 2008 3:21 am

Re: utf8 and mysql... need help!

Post by BETA »

Didn't have much time to try it out... but i did... and omg it worked :D
THX!
Post Reply