reading DB info (special chars)
Moderator: General Moderators
reading DB info (special chars)
hi, I'm making a website, in portuguese, and i'm trying to retrieve characters with accent signals such as é ê á etc.
while when I directly type words with such characters directly in my HTML, it displays correctly (i'm using utf-8), if i save the same words into a mysql DB, and echo them out with php, they will appear with missing characters, you know, the "?" inside a square.
im clueless. any help is appreciated.
thanks in advance
while when I directly type words with such characters directly in my HTML, it displays correctly (i'm using utf-8), if i save the same words into a mysql DB, and echo them out with php, they will appear with missing characters, you know, the "?" inside a square.
im clueless. any help is appreciated.
thanks in advance
Re: reading DB info (special chars)
The easiest solution is to make sure that everything is using the same encoding: the database, tables, and fields (collation); the PHP files themselves; the database client connection (set_charset); and any Content-Type headers or meta tags.
Re: reading DB info (special chars)
tried that, didnt work.McInfo wrote:The easiest solution is to make sure that everything is using the same encoding: the database, tables, and fields (collation); the PHP files themselves; the database client connection (set_charset); and any Content-Type headers or meta tags.
Re: reading DB info (special chars)
I made a simple test for debugging, heres my code:
(I typed "ácéntôão" e "tôrrãodeaçúcar" directly on html for testing, those are exactly the same words I've put on my database.)
my result:
what to do? i tried everything
(I typed "ácéntôão" e "tôrrãodeaçúcar" directly on html for testing, those are exactly the same words I've put on my database.)
Code: Select all
<?php
session_start();
$host_db="localhost";
$name_db="test";
$login_db="root";
$pass_db="123456";
$conn_db=mysql_connect($host_db, $login_db, $pass_db)or die("Could not connect. ".mysql_error());
$select_db=mysql_select_db($name_db, $conn_db)or die("Could not select database. ".mysql_error());
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
ácéntôão<br />
tôrrãodeaçúcar<br />
<?php
$sql="SELECT * FROM test ORDER BY id LIMIT 3";
$result=mysql_query($sql);
if(mysql_num_rows($result))
{
while($row=mysql_fetch_row($result))
{
echo $row[1]."<br />";
}
}
?>
</body>
</html>words in HTML appear exactly as I typed them, but the ones coming from DB appear with those unknown characters.ácéntôão
tôrrãodeaçúcar
�c�nt��o
t�rr�odea��car
what to do? i tried everything
Re: reading DB info (special chars)
See what mysql_client_encoding() returns. Then use mysql_set_charset() to set the encoding explicitly.
Re: reading DB info (special chars)
thanks. my html encoding is utf-8, but client_encoding returns latin1. set charset doesnt appear to work. whats the correct syntax?McInfo wrote:See what mysql_client_encoding() returns. Then use mysql_set_charset() to set the encoding explicitly.
Re: reading DB info (special chars)
Try
MySQL Manual: Character Sets and Collations That MySQL Supports
Code: Select all
mysql_set_charset('utf8', $conn_db);Re: reading DB info (special chars)
will try it now.McInfo wrote:TryMySQL Manual: Character Sets and Collations That MySQL SupportsCode: Select all
mysql_set_charset('utf8', $conn_db);
but while i dont, i just replaced my html encoding to latin1, and it did the trick.
is it a problem to leave it latin1? like some users not being able to see anything?
Re: reading DB info (special chars)
that did it, however, i have to leave it in my code, whenever i remove it, everything goes back to unknown characters.
is it possible to permanently change the database charset?
its weird, because i changed everything on my db manually, to utf-8, but that didnt work, i still see with unknown characters.
is it possible to permanently change the database charset?
its weird, because i changed everything on my db manually, to utf-8, but that didnt work, i still see with unknown characters.
Re: reading DB info (special chars)
mysql_set_charset() defines the character set to be used for the client connection; it does not change the character set of the database. (It is unclear whether you understood that.) That is why its effect is temporary.
Read this chapter of the MySQL manual: Internationalization and Localization.PHP Manual wrote:Note: [mysql_set_charset()] is the preferred way to change the charset. Using mysql_query() to execute SET NAMES .. is not recommended.