7

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Amir
  • 71
  • 1
  • 1
  • 2

9 Answers9

19

I was able to solve this by doing the following:

  1. the db collation has to be utf8_general_ci
  2. the collation of the table with hebrew has to be utf8_general_ci
  3. in your php connection script put header('Content-Type: text/html; charset=utf-8');
  4. in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
amir
  • 191
  • 2
6

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.

DarthJDG
  • 16,511
  • 11
  • 49
  • 56
Pinokyo
  • 546
  • 1
  • 7
  • 15
5

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'.

Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127
3

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

awm
  • 6,526
  • 25
  • 24
  • where do i need to execute it? i tried to execute it in phpMyAdmin in my db under query it returned with syntax error.... i tried putting ; at the end of the query it won't work... i think the problem is with MySQL stuff cause when i echo something in php i can see hebrew.... – Amir Mar 13 '11 at 08:17
  • ok i tried putting mysql_query("SET NAMES utf8"); in my php code it won't work and i tried putting the query to mysql query in phpMyAdmin and still i can't see hebrew characters. this guy in here got it to work but i don't know where he put the codes... http://www.webmasterworld.com/php/3553642.htm – Amir Mar 13 '11 at 09:37
2

$conn->set_charset("utf8"); Use this for your dbconnect

allpnay
  • 539
  • 6
  • 19
1

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

Botz3000
  • 39,020
  • 8
  • 103
  • 127
1

Where are the question marks showing up? It may be an encoding problem somewhere other than in the data base.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • The HTML needs to be generated with an encoding that supports Hebrew (or else the Hebrew characters need to be properly escaped in the HTML). The browser also needs to know what encoding was used (usually through an HTTP header and/or a META tag). It could still be a data base problem, but there are a lot of other places in the path between the data in the data base and the browser display that could be the source of the problem. – Ted Hopp Mar 13 '11 at 16:38
  • thanks ted, but i tried that allready.... when i right click in firefox and selects page info it show that my page uses utf8 but still i see question marks instead of hebrew character's also i have a utf-8 meta tag in my html and lang=he in my header tag. is there any other suggestions?... – Amir Mar 13 '11 at 16:45
  • Make sure that UTF-8 is being used in the code that generates the HTML. Try exercising that code with some hard-coded Hebrew (that doesn't come from the db) to confirm that the problem is in what's coming out of the db. – Ted Hopp Mar 13 '11 at 20:31
  • tried that... for 8 hours i'm trying to see hebrew character's.... when i echo something using php in hebrew i can see hebrew when i write something in hebrew let's say in a h1 tag i can see hebrew that's why i think the problem is in MySQL db... any other suggestion cause i tried all the suggestion u guys gave me but no luck what so ever. thanks in advance. – Amir Mar 13 '11 at 21:42
0

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;
Idan Magled
  • 2,186
  • 1
  • 23
  • 33
-1

mysql_query('SET NAMES utf8') ..... Put this in Php

greatwolf
  • 20,287
  • 13
  • 71
  • 105