I am having trouble displaying Hungarian letters that are read from a database.

Here is the output

and here is the code

<?php
mySQL_connect("localhost","[SNIPPED]","[SNIPPED]") or die (mysql_error()); // connect to mySQL
echo ("Connected to MySQL");
echo ("<hr /><br />");
mysql_select_db("otlethu1_aru") or die (mysql_error()); //connect to otlerhu1_faaru database
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM `Fa` ORDER BY `ID` ASC") or die(mysql_error()); //select all fields from Test table


while ($row = mysql_fetch_array($sql)){
	echo ($row['Product']);
	echo ("<br />");	
}
?>

and here is the same code with some html added

<!DOCTYPE html>
<head>
	<title>Website Name</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel="stylesheet" href="styles.css" media="screen" />
</head>
<body>
	<div class="wrapper">
		<center>
			<div class="header"></div>
			<div class="navbar">
				<div class="button">
					<a href="index.php">Főoldal</a>
				</div>
				<div class="button">
					<a href="products.php">Termékek</a>
				</div>
				<div class="button">
					<a href="about.php">Rólunk</a>
				</div>
				<div class="button">
					<a href="contact.php">Elérhetőség</a>
				</div>
			</div>
			<div class="phpbody">
				<?php
				mySQL_connect("localhost", "[SNIPPED]", "[SNIPPED]") or die(mysql_error());
				// connect to mySQL
				mysql_select_db("otlethu1_aru") or die(mysql_error());
				//connect to otlerhu1_faaru database
				if($_GET['page'] == "fa") {
					echo("<h1>Fa</h1><hr /><br />");
					$sql = mysql_query("SELECT * FROM `Fa`") or die(mysql_error());
					//select all fields from Test table
					while($row = mysql_fetch_array($sql)) {
						echo($row['Product'] . "<br />");
					}
				} else if($_GET['page'] == "villany") {
					echo("<h1>Villany</h1><hr /><br />");
					$sql = mysql_query("SELECT * FROM `Villany`") or die(mysql_error());
					//select all fields from Test table
					while($row = mysql_fetch_array($sql)) {
						echo($row['Product'] . "<br />");
					}
				}
				?>
			</div>
		</center>
	</div>
</body>
</html>

and here is the output for that

Can anyone help me fix this issue? the database has stored them correctly (I can now see the letters, while before some of them were replaced by question marks. View screenshot)

Thanks for any help.

try setting the char code for the html in browser

<head>
<meta http-equiv="Content-Type"
content="text/html;charset=ISO-8859-1" />
</head>

ISO-8859-1 Latin alphabet part 1 North America, Western Europe, Latin America, the Caribbean, Canada, Africa
ISO-8859-2 Latin alphabet part 2 Eastern Europe
ISO-8859-3 Latin alphabet part 3 SE Europe, Esperanto, miscellaneous others
ISO-8859-4 Latin alphabet part 4 Scandinavia/Baltics (and others not in ISO-8859-1)
ISO-8859-5 Latin/Cyrillic part 5 The languages that are using a Cyrillic alphabet such as Bulgarian, Belarusian, Russian and Macedonian
ISO-8859-6 Latin/Arabic part 6 The languages that are using the Arabic alphabet
ISO-8859-7 Latin/Greek part 7 The modern Greek language as well as mathematical symbols derived from the Greek
ISO-8859-8 Latin/Hebrew part 8 The languages that are using the Hebrew alphabet
ISO-8859-9 Latin 5 part 9 The Turkish language. Same as ISO-8859-1 except Turkish characters replace Icelandic ones
ISO-8859-10 Latin 6 Lappish, Nordic, Eskimo The Nordic languages
ISO-8859-15 Latin 9 (aka Latin 0) Similar to ISO 8859-1 but replaces some less common symbols with the euro sign and some other missing characters
ISO-2022-JP Latin/Japanese part 1 The Japanese language
ISO-2022-JP-2 Latin/Japanese part 2 The Japanese language
ISO-2022-KR Latin/Korean part 1 The Korean language

here is the new code

<!DOCTYPE html>
<head> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
</head>

<body>
<?php
define('CHARSET', 'utf-8');
@setlocale(LC_TIME, 'el_GR.utf-8');
mySQL_connect("localhost","otlethu1_default","otl74688") or die (mysql_error()); // connect to mySQL
echo ("Connected to MySQL");
echo ("<hr /><br />");
mysql_select_db("otlethu1_aru") or die (mysql_error()); //connect to otlerhu1_faaru database
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM `Fa` ORDER BY `ID` ASC") or die(mysql_error()); //select all fields from Test table


while ($row = mysql_fetch_array($sql)){
	echo ("<title>Ötlet Barkácsbolt - Fa</title>");
	echo (charcode($row['Product']));
	echo ("<br />");	
}


function charcode ($text) { 
     $text = htmlentities($text); //to convert the simple spec chars 
     $search = array("& otilde;","&O tilde;","& ucirc;","&U circ;"); 
     $replace = array("& #337;","& #336;","& #369;","& #368;"); 
     $text = str_replace($search, $replace, $text); 
     return $text; 
 }
?>
</body>
</html>

I have tried several different character sets like ISO 8859-16

and the result is still the same. In the database it is stored as utf8_hungarian_ci, and all of the letters are correctly visible in the database. I have the HTML extended php code at the same test.php link.

<head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>

OR USE

<?php header("Content-type: text/html; charset=utf-8");?>
<html>
 <head>
  <meta http-equiv="Content-type" value="text/html; charset=utf-8">

Newest code

<!DOCTYPE html>
<head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>

<body>
<?php 
header("Content-type: text/html; charset=utf-8");
mySQL_connect("localhost","[SNIPPED]","[SNIPPED]") or die (mysql_error()); // connect to mySQL
echo ("Connected to MySQL");
echo ("<hr /><br />");
mysql_select_db("otlethu1_aru") or die (mysql_error()); //connect to otlerhu1_faaru database
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM `Fa` ORDER BY `ID` ASC") or die(mysql_error()); //select all fields from Test table


while ($row = mysql_fetch_array($sql)){
	echo ("<title>Ötlet Barkácsbolt - Fa</title>");
	echo (charcode($row['Product']));
	echo ("<br />");	
}


function charcode ($text) { 
     $text = htmlentities($text); //to convert the simple spec chars 
     $search = array("& otilde;","&O tilde;","& ucirc;","&U circ;"); 
     $replace = array("& #337;","& #336;","& #369;","& #368;"); 
     $text = str_replace($search, $replace, $text); 
     return $text; 
 }
?>
</body>
</html>

and the output is still the same.

I also get this error

Warning: Cannot modify header information - headers already sent by (output started at /usr/home/otlethu1/public_html/test.php:6) in /usr/home/otlethu1/public_html/test.php on line 7
 Connected to MySQL

i think the only thing else i can think of is take out what you just added then set the mysql to it when your connecting

$link = mysql_connect(localhost","[SNIPPED]","[SNIPPED]);
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db("otlethu1_aru");
Member Avatar for diafol

It may be overkill, but I always found this to work:

$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use diafol : '. mysql_error());
}
mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_unicode_ci');

.. when I hit encoding problems.

This also goes at the head of my main include file, which is usually the first thing in my pages:

header('Content-Type: text/html; charset=utf-8');

That changed the output the this

á
 é
 í
 ó
 ö
 Å‘
 ú
 ü
 ű

the current collation is

UTF8_hungarian_ci

if that makes a difference. and the current code is

<!DOCTYPE html>
<head> <meta http-equiv="Content-Type" value="text/html;charset=UTF-8" />
</head>

<body>
<?php 
//header("Content-type: text/html; charset=utf-8");
$link = mysql_connect("localhost","[SNIPPED]","[SNIPPED]");
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db("otlethu1_aru");
echo ("Connected to database");
echo ("<hr /><br />");

$sql = mysql_query ("SELECT * FROM `Villany` ORDER BY `ID` ASC") or die(mysql_error()); //select all fields from Test table


while ($row = mysql_fetch_array($sql)){
	echo ("<title>Ötlet Barkácsbolt - Fa</title>");
	echo (charcode($row['Product']));
	echo ("<br />");	
}


function charcode ($text) { 
     $text = htmlentities($text); //to convert the simple spec chars 
     $search = array("& otilde;","&O tilde;","& ucirc;","&U circ;"); 
     $replace = array("& #337;","& #336;","& #369;","& #368;"); 
     $text = str_replace($search, $replace, $text); 
     return $text; 
 }
?>
</body>
</html>
mysql_set_charset('utf8_hungarian_ci',$link);

i dont rember if that was a valid thing to do but give it a whirl cant hurt

ardovs extra code fixed my test one, but when I apply it to the website it changes back to the weird A symbols. why is this. the connection code and everything is the same, I just have an if statement around which table to read from.

UPDATE:

I checked other browsers, and so far it is only working correctly on opera

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.