After reading from my database, special characters are not displayed correctly.
I am connecting to my database in this fashion:
$link = mysql_connect($db_host,$db_user,$db_pass) or die('Unable to establish a DB connection');
mysql_select_db($db_database,$link);
mysql_query("SET names UTF8");
?>
I am reading the database in this fashion:
$rs = mysql_query("SELECT id, name FROM tags ");
while($row = mysql_fetch_array($rs))
{
//Read each tag into a span-menu
echo "<div class=\"span-3\"id=\"".$row['id']."\">"."<h5>".$row['name']."</h5></div>\n ";
}
The table contains correct characters, so the problem is when they are read....
Any ideas?
Thx
It's likely to be your HTML page which is showing the wrong encoding, rather than mysql (or your query).
If you are using HTML5, add the following to the <head> of your html:
<meta charset="utf-8">
If you are using HTML4/XHTML, it's:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
You can see which encoding your page is returning and test different encodings using your browser. In Chrome, click View > Encoding.
Related
PHP is retriving data from MySQL database. Now it's suppose to display the results to screen, but since my database contains characters like (ä,ö,ü,õ), which PHP displays (�,䄔). Database displays all characters correctly, so I don't think the problem is there.
I add following line to my code, that sets charset to UTF-8.
$conn->set_charset("utf8");
After adding that line, PHP displays following characters „”.
Also tried:
$conn->query("SET character_set_results=utf8");
$conn->query("SET NAMES 'utf8'");
How can I get PHP to display correct characters?
Here is the important part of my code
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM employee";
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->bind_result($employee);
while($stmt->fetch()){
echo "$employee|";
}
$stmt->close();
$conn->close();
I'm running on PHP 7 and MySQL 10.1.19-MariaDB.
Consider the following things in this situations.
Make sure the header match the following
Content-Type:text/html; charset=UTF-8
and try setting UTF-8 in html
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
The source file encoding can make problems when string are hard coded in file. Since this file echoing it that seem to be no issue.
When echoing data from the MySQL database I get strange symbols in the text. I've tried htmlspecialchars_decode(), but to no avail. The data is stored as a VARCHAR in the MySQL database and is displayed as they should when queried in MySQL workbench.
The characters include ', ë, è, é, ê, ...
How do I get these characters to display in html?
Have you declared a charset in the HTML of your page?
In HTML5 this is fine:
<meta charset="UTF-8">
Older (HTML 4.01) needs something like:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
You have encoding issues.
You need to set <meta charset="UTF-8">.
You need to run query after you set up connection to DB "SET NAMES utf8"
If you using PDO need set PDO like this new M_PDO("mysql:host={$dbhost};dbname={$dbname};charset=utf-8", $dbuser, $dbpassword);
Also you can use this function which will encode accents to html entities
function convert($str){
return mb_convert_encoding($str, "HTML-ENTITIES", "UTF-8");
}
echo convert($YourDataFromDB);
Hope it will helps
This worked for me:
$db = Database::getInstance();
$mysqli = $db->getConnection();
$mysqli->set_charset('utf8mb4');
Stolen from UTF-8 all the way through
I've got a mysql table with some imported data, in particular one value is Sinn Féin.
The character set used for my database is utf8_general_ci. The data displays fine in phpMyAdmin. In my site, I've used the PHP header header("Content-type: text/html; charset='utf-8'");. I've got <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> in my <head>.
My data still comes up as Sinn F�in. I've tried using utf8_decode, but that doesn't help. What am I doing wrong?
Аfter mysql_connect() add this line:
mysql_query ("SET NAMES utf8");
Try this:
$dbc="database connection";
mysql_query("SET NAMES utf8",$dbc);
I have the following php code:
<?php
mysql_connect("localhost","root","*****");
mysql_select_db("MyData");
$sql=mysql_query("select * from menu");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));
mysql_close();
?>
My data base is encoded on utf8_unicode_ci.
So when I read the output in a browser the latin characters a presented well but greek characters are presented with "???????".When I read the data from my data base are presented regularly.
Can someone help me about what do I have to do with my php code?
add these 2 queries to after mysql_select_db("MyData"); :
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
Try
header('Content-type: text/plain;charset=UTF-8');
You'd need to do this before the print() call in your script.
PHP :
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES utf8");
HTML :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
File : Convert file to UTF-8 using notepad
Have a look at your html encoding, because it is likely to be ISO-LATIN-1 default and thus your display is the issue not the sql.
In that case you want to do the following
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head> <?php print(json_encode($output)); ?>
</html>
I solved it in a similar way as MBarsi, adding a line of code after selecting the database, but using mysqli:
$mysqli->select_db("database");
$mysqli->set_charset("utf8");
Problem, simple and annoying.
Im just trying to print a list of names, collected from my mysql database.
The PHP-files are saved in utf8, the database and tables are set to use utf8. Still 'å,ä,ö', for example, outputs as �. Can't believe I'm still having this issue.
Of course, Latin1 solves the problem. The thing is that I have to use utf8 since I'm doing some json_encode for sending the data to an ajax-script.
Any idea what on earth could be wrong?
Should I convert the data to utf8 before returning it perhaps? Seems weird I should have to..
Convert utf8_general_ci to utf8_unicode_ci...
Try running SET NAMES UTF8 query after you connect to database...
function connect($server, $database, $username, $password, $charset = "UTF8"){
$link = mysql_connect($server, $database, $password);
if(!$link){
die("Unable to connect to database server.");
}
mysql_selectdb($database);
if(function_exists("mysql_set_charset")){
mysql_set_charset($charset, $link);
}else{
mysql_query("SET NAMES $charset");
}
}
Also make sure you have this (or via header()) in your HTML...
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Two things to do...
Make sure your HTML header is sending the correct charset:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Use utf_encode() when adding your names to the array. The line in your should be
$guests[] = array_map('utf8_encode', $row);