I have a PHP file encoded with UTF-8 like this :
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
And before using sql request, I've added this line :
mysql_query("SET NAMES 'utf8'");
My database is coded in UTF-8 and varchar column with latin_swedish_ci
The result is like this picture :
Chang code to this
mysql_query("SET NAMES UTF8");
Save file like this
This is due to incorrect collation / charset of your column. Even if your database characted set is UTF-8, the column's character set takes precedence. To handle special characters like japanese chinese you column should be set to utf8_bin instead of latin_swedish_ci
Some Steps to Follow:
Meta tag for UTF8.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8.
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For MySql, you've to convert your table to UTF8.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
Also run:
SET NAMES UTF8
as the first query after establishing a connection which will convert your DB connection to UTF8.
html encoding in meta tag gets ignored if it is sent via http headers already.
header('Content-Type: text/html; charset=utf-8');
Check from you browser what it thinks the current page encoding is:
Chrome: Tools - Encoding
Firefox: View - Character encoding
MySQL should be ok with just "set names utf8", table dada is encoded automatically to the mysql connections encoding
Related
I am having issues using hebrew with mysql and PHP, i have already tried this:
the db collation has to be utf8_general_ci.
the collation of the table with hebrew has to be utf8_general_ci
in my php connection script i put
header('Content-Type: text/html; charset=utf-8');
in my xhtml head tag i put
after selecting the db in the connection script i put
mysql_query("SET NAMES 'utf8'");
And it is still not working. In phpmyadmin i can see the hebrew characters but when i display them in php all i see is "?????"
If anyone could help me that would be great!
In your html page
Under <head> tage, add <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
At the time of mysql insert use utf8_encode & when show data try to use utf8_decode
I have a table which contains name of teachers. Some names are like René Visser having special characters. When I write the SQL query for displaying the name, the special characters are replaced by � symbols.
I have tried cast() but it's not working properly. My query is like this.
$qry = mssql_query("SELECT CAST(FirstName_1 AS NVARCHAR(250)) AS Name FROM
tbl_teachers");
The FirstName_1 column is nvarchar type. I have tried to cast FirstName_1 to VARBINARY(8000), then casting result to IMAGE like following.
CAST(CAST(FirstName_1 AS VARBINARY(8000)) AS IMAGE) AS Name.
You should have UTF-8 encoding for the SQL server.
Then, make sure you send the encoding headers also from php using :
header('Content-Type: text/html; charset=utf-8');
You will need to specify the charset, or if you have already, set it to Windows-1252. It's likely your page is reading in the data with UTF-8 encoding. Which explains the ? symbols.
<head>
<meta charset="Windows-1252">
</head>
You most likely have a charset issue. Your query has little to do with this, you don't need to cast it.
You'll need to set the charset of the connection, the PHP and HTML header and the document itself as the same charset. UTF-8 will most likely cover all of the special characters you'll ever need.
Below is some things you could do.
ini_set('mssql.charset', 'UTF-8'); (Have this run upon connecting to your database)
Set both PHP and HTML header to UTF-8
PHP: header('Content-Type: text/html; charset=utf-8'); (has to be called before any and all output)
HTML: <meta charset="utf-8"> (has to be inside the <head>-tag
Save the document in UTF-8 encoding. If you're doing it in Notepad++, it's Format -> Convert to UFT-8 (you may also choose UTF-8 w/o BOM)
The database itself, and it's tables, may need to be set to UTF-8. This can be done with the query below (need only to be run once):
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Keep in mind that all parts of your application has to be set to the same type of charset, or you'll experience those kind of things in your database.
I am having problems displaying foreign characters (characters with accents like: é à ù ç and so on)
The row in the database is like this:
Name | Datatype | Charset
title | varchar(255) | utf8_general_ci
I store it like this:
function inputFilter($var)
{
$var = trim(htmlentities(strip_tags($var)));
if (get_magic_quotes_gpc())
$var = stripslashes($var);
$var = mysql_real_escape_string($var);
return $var;
}
$title = inputFilter($_POST['title']);
and I print it like this:
print $getfromdb['title'];
This is how it's printed out:
Português //Should be: Português
I have tried adding:
htmlspecialchars, utf8_decode/encode and htmlentities to the print, although nothing helps!
I've added this to the header:
<meta charset="utf-8">
What am I doing wrong?
Steps to Follow:
Use the meta tag for UTF8.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8.
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For mysql, you want to convert your table to UTF8.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
Also you can run:
SET NAMES UTF8
as the first query after establishing a connection which will convert your DB connection to UTF8.
Include mysqli_set_charset($link, "utf8"); right after every connection you make. This will work.
Try this:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8"> is HTML 5 (and, BTW, UTF-8 is uppercase)
Since it looks like your test chain also involves a form (because of the $_POST), you must make sure that the UTF-8 charset is set for the form too.
Use SET NAMES utf8 before you query/insert into the database
query("SET NAMES utf8");
You should use character encoding as ISO-8859-1 instead of UTF-8 as follows:
<meta charset="ISO-8859-1">
The characters you are trying to show are latin and UTF-8 i.e. UNICODE encoding cannot interpret latin characters.
Reference
And in case of mysql you should use latin1 charset.
This is really driving me crazy. I'm building a spanish website (meaning a lot of latin characters) and I'm using Zend framework.
When I save a á it displays like á . I know is a charset encoding but I dont understand why.
My head charset is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
My database is utf8_general_ci . I've changed the charset to others and always the same problem.
When I look in my database, what is saved is an á
Any idea why is this happening? Thanks!
Here goes the recipe to get rid of encoding headaches:
Your DB, tables and fields must use collation utf8_unicode_ci.
Be sure your code (HTML, PHP... all) is UTF-8 encoded.
Always use this meta tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
This is long:
If you have access to MySQL file configuration my.cnf just add this line:
init-connect='SET NAMES utf8'
This tells MySQL to return results in UTF-8 for each connection.
If you cannot edit my.cnf but you are using PDO, open the connection this way:
$pdo = new PDO($dsn, $usr, $pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
If you aren't using PDO... start using it, what are you waiting for? Meanwhile you can execute this after each connection if you use ext/MySQLi:
$mysql->set_charset('utf8');
Or this if you use plain old ext/MySQL:
mysql_set_charset('utf8', $connection);
The character á and other characters outside the ASCII map can be mapped with unicode.
As you are stroing the data in a table with UTF-8 charset and characters are storing properly.
I would suggest you that
Before you store data
Before you fetch data
Run the query SET NAMES UTF-8. This will fetch your UTF-8 data with taking care of the characters which are outside of ASCII map.
While outputting the data you make sure that you are forcing/directing browser to use the UTF-8 charset to print the data.
Why insert in database value date as: شهريور ?
How can search this word in database(شهريور)?
In database: structure => date => varchar(255) => utf8_general_ci = "شهريور".
You website uses an encoding in which these characters do not exists, so the browser sends HTML entities instead.
(Try this here: http://codepad.viper-7.com/dfFMvW ; This page is in ISO-8859-1, if you send non-ISO-8859-1 characters in the input, they are sent as HTML entities.)
To avoid this you have to use a different encoding, like UTF-8.
Add this header in your <head> tag:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
Or do this in your PHP before printing anything:
header('Content-Type: text/html; charset=UTF-8');
And make sure your database uses UTF-8 too.
You can convert your database to UTF-8 by doing this:
ALTER DATABASE your_database CHARACTER SET utf8;
-- for each table:
ALTER TABLE some_table CONVERT TO CHARACTER SET utf8;
And after you connect to the database, send this query:
SET NAMES UTF8;
You dont need to html escape those characters as long as you have a UTF* table, and you do.
Simply make sure that the table is UTF8, that the connection is utf8, and the browser reads the texts as utf8.
for mysql see SET CHARACTER SET, SET NAMES, SET COLLATION_CONNECTION
for html use <meta content="text/html;charset=UTF-8" http-equiv="Content-Type" /> and the according http headers, if needed