Persian words in the database and it is issue? - php

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

Related

Display special characters from table in php page

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.

PHP MYSQL special characters do not show good

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

php/mysql - foreign characters

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.

UTF8 mysql encoded database, but not showing special characters in PHP

I have a database with utf_general_ci encoding in MySQL, when I insert data in Navicat or PhpMyAdmin and do queries there, the special characters are returned fine.
But in a php variable they are showing like this:
My database and collations:
and in my html doc if it is a normal it shows the characters OK, it only not work when is a php variable
please help me
Try:
$variable = utf8_encode($valueCommingFromDB);
It's important to note that utf8 in mysql is fairly limited, especially utf8_general_ci. You might want to switch to utf8_unicode_ci or utf8mb4
What's the difference between utf8_general_ci and utf8_unicode_ci
http://mathiasbynens.be/notes/mysql-utf8mb4
Hei,
Firstly be sure you have this meta in html tag
<meta charset="UTF-8">
You can check also with this line in php, above you mysql extraction
header('Content-type: text/html; charset=utf-8');
If you store your special character in database as html code like this ' which is ' when you extract try to use this php function
htmlspecialchars_decode($your_text_from_db);

UTF-8 page to UTF-8 database table showing incorrect

I'm hoping for an explanation of why some UTF-8 text is being saved to a database table incorrectly...
I created an HTML form and the page's meta content is set to UTF-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
The PHP and template files are all Unicode/UTF-8.
The form field data is submitted to a utf8_unicode_ci encoded database table.
If I submit the form with characters such as "éçä" (which I created from Windows' Character Map program set to Unicode character set) they show up incorrectly in the database ("éçä"). I'm viewing the database via phpMyAdmin (which is also set to UTF-8 character encoding).
However, if I run iconv() on the string to convert to ISO-8859-1 before inserting it into the database, then the character show correctly:
$input = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $input);
What is going on? Shouldn't the fact that everything is UTF-8/Unicode from beginning to end resulted in it being correct in the DB? What am I doing wrong and why did converting the data to ISO-8859-1 work?
The only other thing done to the data is a FILTER_SANITIZE_MAGIC_QUOTES:
$input = filter_var($input,FILTER_SANITIZE_MAGIC_QUOTES);
Thank you for your time and input.
Two steps you haven't mentioned:
Specify UTF-8 in HTTP Content-Type header
Specify UTF-8 when connecting to MySQL, e.g. specifying charset in PDO

Categories