I'm trying to build a shopping cart using PHP & MySQL.
my db in MySQL is utf8 and my table in the db is utf8,
How can I use Hebrew characters?
I was able to solve this by doing the following:
the db collation has to be utf8_general_ci
the collation of the table with hebrew 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'");
After a lot of work I found a solution that always works..:
without SET_NAMES.
In the conn.inc.php file, after you selected a database and connected to it, do this:
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}
...and in the html always use charset utf-8;
That solved it for me. No need to use set_names(), which is ok but it annoyed the hell out of me.
You can use the PDO in your code like this:
$db = new PDO($config['DSN'], $config['dbUserName'], $config['dbPassword']);
$db->exec("SET NAMES 'utf8'");
Make sure you include the single quotation marks around the 'utf8'.
If it's an encoding problem (and it sounds like it is), this query will help:
SET NAMES utf8
Execute this query (e.g. mysql_query("SET NAMES utf8")) right after you connect and before you move any data.
More info
$conn->set_charset("utf8"); Use this for your dbconnect
Where are the question marks showing up? It may be an encoding problem somewhere other than in the data base.
To store non-ASCII characters in a database column, you need to define that column with a specific character set. You can specify a character set at 3 levels: database, table, and column. For example:
CREATE DATABASE db_name CHARACTER SET utf8
CREATE TABLE tbl_name (...) CHARACTER SET utf8
CREATE TABLE tbl_name (col_name CHAR(80) CHARACTER SET utf8, ...)
http://www.herongyang.com/PHP/Non-ASCII-MySQL-Store-Non-ASCII-Character-in-Database.html
In my case I created the DB from a dump, and this command solve it:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql_query('SET NAMES utf8') ..... Put this in Php
Related
I'm trying to store an Arabic text to the table, I searched a lot but I didn't find a solution that worked for me, so this is what I got:
$en = "OK";
$ar = "حسناً";
$link->query("INSERT INTO words (en,ar) VALUES ($en,$ar)");
The problem is when I insert it, the Arabic text looks like Øسناً, my table's collation and MySQL's are utf8_general_ci, so is my database's, I also have mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');, but it doesn't work.
I recently had the same issues myself.
Here's a few pointers:
ALL attributes must be set to ut8 (collation is NOT the same as charset)
Save the document as UTF-8 (If you're using Notepad++, it's Format -> Convert to UFT-8)
The header in both PHP and HTML should be set to UTF-8 (HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and PHP: header('Content-Type: text/html; charset=utf-8');
Upon connecting to the databse, set the charset ti UTF-8 there as well, like this: $link->set_charset("utf8"); (directly after connecting)
Also make sure your database and tables are set to UTF-8, you can do that like this:
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Remember that EVERYTHING needs to be set to UFT-8 charcode, or else it'll insert stuff like "Øسناً". Hope this helped!
First thing is the database, table, and column. See if utf8 is set:
utf8_general_ci or utf8_unicode_ci
Also, the connection collation: utf8_general_ci
In PHP, after I connect with mysqli, I issue this:
$conn->query("SET NAMES 'utf8'");
And the web page output always jams in:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Here is a solution that worked for me:
when you create the database choose the collation for: utf8_unicode_ci
when you connect to the database from php use the following character set:
$dsn = "mysql:host=localhost;dbname=record;charset=utf8";
I have a form on a PHP page which inserts data into a MySQL database.
Some input fields may contain UTF8 characters as é, è, â, etc. When they are actually inserted into the database, everything gets messed up. For example, a column shows Qréon instead of Qréon.
I used setLocale(LC_CTYPE, 'FR_fr.UTF-8'); at the top of my page for PHP and this <meta charset="utf-8"> is in my HTML header.
My database is run by MySQL, the storage engine is InnoDB, and the collation is utf8_general_mysql500_ci. I also tried utf8_general_ci and utf8_bin but I had no luck.
How do I know if this comes either from PHP or MySQL processing, and how can I fix it ?
Thank you for your time.
I think this can help you:
If your using mysql
mysql_query("SET NAMES 'utf8'");
If your using PDO use this:
$dbh->exec("set names utf8");
Otherwise i could be one of these which helps you specific:
//At the Top of you files
ini_set("default_charset", "UTF-8");
header('Content-type: text/html; charset=UTF-8');
//Before your queries
mysql_query("SET CHARACTER SET utf8 ");
mysql_set_charset('utf8');
I'm trying to insert an hebrew value into my mysql db, instead of hebrew the values looks like that.
שדגשדכעשד
The collation of the table is latin1_swedish_ci by default, I tried also to change to utf-8_general_ci, hebrew_bin, hebrew_general_ci but the result is still the same.
In my code I'm using of course the meta tag to configure the charset:
<meta charset="UTF-8">
And before my php query I added this line:
mysql_query("SET NAMES utf8");
I'm viewing the result in the phpmyadmin.
I have solved my Hebrew language problem. It was a database and table row/field encoding issue. Here is the solution I used. I took help from another answer and the link is given below, in case anyone needs it.
The database collation has to be utf8_general_ci.
The collation of the table with Hebrew has to be utf8_general_ci.
In the PHP connection script put
header('Content-Type: text/html; charset=utf-8');
In the xhtml head tag put
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If you are using MySQLi put this code in the connection script after selecting the database:
mysql_query("SET NAMES 'utf8'");
If you are using PDO, put
$conn->query("SET NAMES 'utf8'");
The first answer helped me and I took it from there
Set charset to achieve the solution
While creating the database
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
Or if the database is already created
CREATE TABLE table_name(
...
)
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
OR while writing query
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";}
Check the collation_connection:
show variables like '%collation%'
you should make sure that:
you set utf-8 in php
you use utf-8 in the connection
your table is defined as utf-8_general_ci
the specific field is defined as utf-8_general_ci
Then you should be able to view Hebrew, or any other language, correctly in phpadmin
what finally helped me is to add the charset to the connection:
{"mysql:host=$host;dbname=$db;charset=utf8"}
I would say, in order to make sure that the values are passed well to the database I would add a die statment before inserting to the database and print the value, example:
die($_POST['thevalue']);
//insert to database.
//...
If it goes well, then the problem is on the database side, on the database I would try with this collation
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
as per http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html suggest.
But if it fail on the php side, reason can be, the server does not support Hebrew, make sure that on the html output document you use the correct metatag with
...
<meta charset="ISO 8859-8">
...
Let us knows how it proceed, good luck :)
For future use, if you have this issue and you are using PDO and not mysqli, you will need to do it like this:
The Database (all of it) collation has to be utf8_general_ci.
Set the collation of the table with Hebrew also to utf8_general_ci.
In the HTML "head" tag, add this: < meta charset="utf-8" >
In your PHP connection file, add after the connection Query this line: conn->exec("set names utf8");
if you are using classes, then you will probably have the "conn" as a variable.
in that case, you will have to use it like this:
$this->conn->exec("set names utf8");
Hope this helps to future people that have this problem and using PDO.
Best of luck.
I think I figure it out:
here's my code:
$conn = mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname);
if (mysqli_connect_errno()){
printf("Connection failed: %s\n" , mysqli_connect_errno());
exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($conn));
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "hebrew")) {
printf("Error loading character set hebrew: %s\n", mysqli_error($conn));
exit();
} else {
printf("Current character set: %s\n", mysqli_character_set_name($conn));
}
In MySQL I have a string, which is displayed properly in phpmyadmin, with the collation set to utf8_general_ci .
In the PHP file, I have set the header using plain HTML:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
The php files are hosted on Ubuntu, Apache 2.2, and the files are saved in UTF-8 without BOM (using Notepad++)
I have set the connection to use utf8:
#mysql_query("set names 'utf8'", $this->link);
$r = #mysql_query($query, $this->link);
it returns the string ( varchar 255 ) and using PHP echo , it is displayed to the browser.
However, I am still getting ????? instead of the actual words.
I can't seem to be able to searched for what I might have missed, your help is much appreciated.
===== Edit =====
Database, table and column can have three different collations, I have set them all to utf8_general_ci , and calling mysql_set_charset('utf8') immediately after the connection is made, have solved my problem.
I hope this will act as a useful checklist for future people bumping into the same problem. I for one may very well return in a few months to check if I have missed anything :)
Thanks all for the great input.
Be sure your table is in utf8_general_ci.
Database and tables can have a different charset
(can happen when you change it by yourself).
And be careful with some functions, like htmlentities,
which have a default charset parameter set to ISO-8859-1 for PHP <5.4.0
(set to UTF-8 for PHP >5.4.0)
http://php.net/manual/en/function.htmlentities.php
I think the quotes around 'utf8' are wrong, but either way, you can just use:
mysql_set_charset('utf8');
And suppressing errors with the # operator when you are looking for them, is not a good idea.
Check something like this before the query
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_general_ci');
you can use this to encode the strings to UTF-8 so it will not get ???, but will change the non urf-8 characters like this /u003. so you can decode it later to present it on a text
while($var=mysql_fetch_assoc($r)){
$e['columnI']=utf8_encode($e['columnI']);
I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.
Unfortunately I can't get it to work.
My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.
(it is special characters like æ/ø/å that doesn't work)
Hope you guys can help!
Make sure the connection to your database is also using this character set:
$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);
According to the documentation of mysql_set_charset at php.net:
Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.
See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php
Check the character set of your current connection with:
echo mysql_client_encoding($conn);
See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php
If you have done these things and add weird characters to your table, you will see it is displayed correct.
Remember to set connection encoding to utf8 as well.
In ext\mysqli do
$mysqli->set_charset("utf8")
In ext\mysql do
mysql_set_charset("utf8")
With other db extensions you might have to run query like
SET NAMES 'utf8'
Some more details about connection encoding in MySQL
As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.
After connecting to db, run query SET NAMES UTF8
$db = new db(...);
$db->query('set name utf8');
and add this tag to header
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then
SET character_set_connection = utf8;
Try this after connecting to mysql:
mysql_query("SET NAMES 'utf8'");
And encode PHP document in UTF-8 without BOM.
I had the same problem but now its resolved. Here is the solution:
1st: update ur table
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
2nd:
add this in the head section of the HTML code:
Regards
Saleha A.Latif
Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"