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);
Related
I am trying to display chinese characters that I have saved in MySQL database, which is saved under utf8_unicode_ci type. I have seen several solutions on the web, but nothing works.
Below is my connection file:
$conn = mysql_connect("localhost","root","password");
mysql_set_charset('utf8',$conn);
mysql_query("SET CHARACTER SET utf8 ");
mysql_select_db("database");
Below is my query:
mysql_query("SET character_set_results=utf8", $conn);
$sql = mysql_query("select * from webdata",$conn);
But it still shows ????. Any ideas?
How to resolve...
When I had a similar issue I firstly displayed the encoding of my text in php using
echo mb_detect_encoding ( $text );
It shows the encoding of the text coming from my query. This showed me that I was getting ASCII from mysql_query when Chinese or Russian characters were in my database.
The change I made was with the following addition after the mysql_connect
mysql_set_charset('UTF8');
My database is utf8_unicode_ci collation and I can see chinese characters.
So, if mb_detect_encoding is now giving you UTF8 for your text then you would be able to show chinese characters.
The next step is to make sure what you pass to the browser has the correct header...
header('Content-Type: text/html; charset=UTF-8');
Put the above at the top of your code in php to make sure the browser is expecting your encoding.
Now that should the question, however ideally you should be using PDO or mysqli rather than mysql_connect method. In this case the equivalent procedural style commands are..
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
mysqli_set_charset($link, "utf8");
Where $link is the equivalent to your connection to the database.
where it show "???", when you print the output to HTML ?
if so, try to add to <head> element the line
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
hope it helped a bit.
EDIT
it seems that you need to declare UTF8 on:
character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8'"
checkout
PHP UTF8 not displaying chinese characters properly
That should be all you need. Both for Traditional and Simplified Chinese characters
1. Make sure your table is set to COLLATION utf8_general_ci
2. $con = new mysqli("localhost",$username,$password,$database) or die("Error " . mysqli_error($con)); $con->query("SET NAMES 'utf8'");
3. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
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 have a database which is encoded in UTF8_bin.
Whenever I try to echo something on that database I get questionmarks instead of letters. Anyone knows a solution for that? I think it is important to mention that if I do echo to a word in UTF8 it is just fine. The problem is getting the data from the database.
Please check whether you have followed these steps.
-> db collation must be utf8_general_ci
-> then collation of the table with language has to be utf8_general_ci
-> in your php connection script put header('Content-Type: text/html; charset=utf-8');
-> in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-> after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
Then check the connection like this,
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}
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 a field, $name (varchar(128)) in which there's a value "Pølse Mand".
Now I want it to print something special whenever the value is "Pølse Mand"
<?php
if ($name=="Pølse Mand") { echo "123";};
?>
But for some reason this doesn't work. I also have another value named "Text box" and it works fine when I do the same with that, so it must be the "ø" that messes things up.
I assume my "ø" in the php file is somehow different from the ø in the value, even though I've tried copying it directly letter for letter in phpmyadmin.
The MySQL connection collation is utf8_unicode_ci and the collation in the table is latin1_swedish_ci. I have tried: <meta http-equiv="Content-Type" content="text/html; charset=utf8_unicode_ci"/> and with the swedish one, but it just doesn't work.
Are you using MySql or MySqli?
For MySQL use this code before the request:
mysql_set_charset ( 'latin7' );
or:
mysql_set_charset ( 'utf8' );
And for MySQLi:
$mysqli -> set_charset ( 'latin7' ); //Change $mysqli to your variable name
or:
$mysqli -> set_charset ( 'utf8' );
As for the <meta> tag, use:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
or:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-13"/>
Try both options and find which works best for you.
You're using UTF-8 on the client side so must tell MySQL that you use UTF-8. This is done by the MySQL command SET NAMES 'utf8' which must be send as the first query on opening a connection. Depending on your installation and on the MySQL client library you use in the PHP script this can be done automatically on each connect.
$mysqli = new mysqli("localhost", "user", "password", "db");
$mysqli->set_charset("utf8");
//
$mysql_set_charset("utf8");
try to change table encoding to utf8 and after mysql connection run query: set names utf8