Page 1 of 2
Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 10:49 am
by nyo
Hi all,
I started to work with a database and want to display some data on my web page. I established database connection and now displaying one field of data. The problem is with the encoding. The
Collation value for the data field I am displaying is
latin1_swedish_ci and I want to display it properly but it doesn't show up as it should. Here is the display page: http://mobile.melekapart.com/ziyaretci-defteri.php5 Lists the names in the database. Field name in the database is
authorname.
Here is my code:
<?php
$db_host = "hostname";
$db_username = "username";
$db_pass = "password";
$db_name = "dbname";
$db = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");
mysqli_set_charset($db, 'utf-8');
$result = mysqli_query($db, 'SELECT authorname FROM tablename');
while ($row = mysqli_fetch_array($result))
{
$authornames[] = $row['authorname'];
}
?>
<?php foreach ($authornames as $author): ?>
<p><?php echo $author; ?></p>
<?php endforeach; ?>
Any ideas?
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 1:59 pm
by jacek
Try changing the coalition to the unicode one, I think its last on the list.
also make sure the encoding of your page is set to UTF-8.
you may need to re-insert the data
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:07 pm
by nyo
The same database is used on another page of the website and all characters look ok. It was coded long ago by another person and it is a bit complicated for me at this time that I cannot figure out how he did it.
I tried changing to utf8_unicode_ci but it didn't help.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:13 pm
by jacek
Can you put the link to the page back ?
If you set everythign to UFT-8 the characters should work.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:33 pm
by nyo
http://mobile.melekapart.com/ziyaretci-defteri.php5
Someone mentioned using the following function. I did and now the diamonds are gone but again some of the special characters are not displaying as they should.
utf8_encode($comment['author'])
You can see the other page which shows the database nicely here: http://melekapart.com/tr/ > Ziyaretçi Defteri
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:36 pm
by jacek
If you view the data in the table using phpmyadmin, does it look like it should or is it broken there too.
you should not need to use any php functions on the data to get it to display.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:45 pm
by nyo
Nice question, it looks broken in phpmyadmin too. Doesn't show "question mark in diamonds", but it looks the same as it is on the page now http://mobile.melekapart.com/ziyaretci-defteri.php5. Some of the Turkish characters like ş, ğ, ı are broken but others are ok.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 2:50 pm
by jacek
Do you have a correct copy of the text anywhere, you may need to update it in the DB again using phpmyadmin.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:13 pm
by nyo
Hmm, as I said, there is a correct copy of all that text on another page of the site. I will try changing them through phpmyadmin now. Thanks for the suggestion. But it is a guestbook and what about future entries?
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:15 pm
by nyo
jacek wrote:Do you have a correct copy of the text anywhere, you may need to update it in the DB again using phpmyadmin.
No it didn't work.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:28 pm
by jacek
Well I was thinking that when you changes the encoding in phpmyadmin for the table it may have broken the data somehow, but I guess not
the special characters appear on the page for me though, what is the issue ? I don't know how they should look
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:34 pm
by nyo
It looks ok on this page: http://melekapart.com/tr/ > Ziyaretçi Defteri
Samet bey merhaba... Aslında çokta paylaşabileceğimiz birşey yok... Neden diyeceksiniz, çünkü biz kaldığımız 1 hafta boyunca sürekli gezdiğimiz için aparta sadece yatmaya geliyorduk diyebilirim Genel olarak çok sakin, temiz ve kanımızın kaynadığı bir yer oldu Ailecek çok sevdik. Şunu diyebilirim ki, artık fethiyede ki adresimiz Melek Apart. Mutlaka ama mutlaka yolumuz yine düşecektir apartınıza... Sevgiler...
It doesn't look ok on this page: http://mobile.melekapart.com/ziyaretci-defteri.php5
Samet bey merhaba... Aslýnda çokta paylaþabileceðimiz birþey yok... Neden diyeceksiniz, çünkü biz kaldýðýmýz 1 hafta boyunca sürekli gezdiðimiz için aparta sadece yatmaya geliyorduk diyebilirim Genel olarak çok sakin, temiz ve kanýmýzýn kaynadýðý bir yer oldu Ailecek çok sevdik. Þunu diyebilirim ki, artýk fethiyede ki adresimiz Melek Apart. Mutlaka ama mutlaka yolumuz yine düþecektir apartýnýza... Sevgiler...
correct: ğ, wrong: ð
correct: ı, wrong: ý
correct: ş, wrong: þ
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:39 pm
by jacek
Are you still the utf8_encode function ?
Maybe post the code for that page ?
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 3:50 pm
by nyo
ziyaretci-defteri.php5
<?php include 'includes/header.php'; ?>
<?php
function formatdate($timeformat,$timestamp) {
global $timezoneoffset;
$summertime = date("I")*3600;
$timestamp+=3600*intval($timezoneoffset)+$summertime;
return gmdate($timeformat, $timestamp);
}
$db_host = "dbhost";
$db_username = "uname";
$db_pass = "pass";
$db_name = "dbname";
$db = mysqli_connect("$db_host","$db_username","$db_pass", "$db_name") or die ("could not connect to mysql");
mysqli_set_charset($db, 'utf-8');
$result = mysqli_query($db, 'SELECT authorname, message, posttime, visible FROM bgb2_posts WHERE visible = 1 ORDER BY posttime DESC');
while ($row = mysqli_fetch_array($result))
{
$comments[] = array('author' => $row['authorname'], 'message' => $row['message'], 'time' => $row['posttime']);
}
$posttime = formatdate($timeformat, $comments['time']);
?>
<div id="title"><a href="tr.php">Ana Sayfa</a> » Ziyaretçi Defteri</div>
<div id="content">
<?php foreach ($comments as $comment):
echo "<p class='textbold'>" . utf8_encode($comment['author']) . " - " . $posttime . "</p>";
echo "<p>" . utf8_encode($comment['message']) . "</p>";
endforeach;
?>
</div>
<?php include 'includes/footer.php'; ?>
header.php
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml">
<?php include 'includes/functions.php'; ?>
<head>
<title><?php sitename(); ?> Fethiye - Turkey</title>
<meta http-equiv="content-type" content="application/xhtml+xml;charset=utf-8" />
<meta http-equiv="cache-control" content="max-age=300" />
<meta name="description" content="Melek Apart Hotel Fethiye - Turkey" />
<meta name="keywords" content="fethiye,çalış plajı,calis beach,ölüdeniz,fethiye apart,fethiye otel,fethiye hotel" />
<meta name="HandheldFriendly" content="True" />
<meta name='viewport' content='user-scalable=no, width=device-width' />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<a href="index.php"><div id="logo"><img src="images/logo.png" /></div></a>
</div>
All files are saved with utf8 encoding in Notepad++.
By the way, I am also trying to display time but cannot get it working. It's in this format in the database: 1211375284
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:07 pm
by jacek
if you remove the utf8_encode function, what happens ?
the time thing is a question for another topic
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:14 pm
by nyo
I removed it and it is again question mark in diamonds. http://mobile.melekapart.com/ziyaretci-defteri.php5
What a small issue takes such a long time to fix in coding. I both hate and love it.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:26 pm
by jacek
Try
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
instead of what you have.
I downloaded a copy of your page, and using this and saving the file with UTF-8 I was able to get the characters to display correctly.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:40 pm
by nyo
No, didn't work. I will continue trying tomorrow... Thanks for your help.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:46 pm
by jacek
I guess it could be caused by the server or your FTP client or something.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:47 pm
by nyo
Maybe. I am uploading files with FTP (Filezilla) because the hosting doesn't have a file manager.
Re: Dislaying Data Properly Using MySQL
Posted: Fri May 13, 2011 4:53 pm
by jacek
Ah, according to
http://php.net/manual/en/mysqli.set-charset.php
You should use
mysqli_set_charset($link, "utf8")
ie utf8 not utf-8
does that help ?