12

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.

Imri Persiado
  • 1,857
  • 8
  • 29
  • 45

8 Answers8

21

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.

  1. The database collation has to be utf8_general_ci.
  2. The collation of the table with Hebrew has to be utf8_general_ci.
  3. In the PHP connection script put

    header('Content-Type: text/html; charset=utf-8');
    
  4. In the xhtml head tag put

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
  5. 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

Sarkar Raj
  • 261
  • 3
  • 12
  • 1
    Sardar Raj this was the magic spot for me: mysql_query("SET NAMES 'utf8'") +1 – Itai Spector Oct 31 '15 at 21:32
  • You just need to set the Collation of the column you are saving the hebrew text into to `utf8_general_ci`, the database and table defaults don't matter – Bim Jun 10 '23 at 15:12
6

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 />";} 
bummi
  • 27,123
  • 14
  • 62
  • 101
Praveen D
  • 2,337
  • 2
  • 31
  • 43
4

Check the collation_connection:

show variables like '%collation%'
dwjv
  • 1,227
  • 9
  • 15
  • I'm sorry, I didn't understand your answer. can you add some more details? Thanks. – Imri Persiado May 30 '13 at 13:01
  • 1
    You need to do some troubleshooting, execute the query I suggested from your PHP script (and display the results) to make sure all the collation settings are correct. – dwjv May 30 '13 at 13:22
1

you should make sure that:

  1. you set utf-8 in php
  2. you use utf-8 in the connection
  3. your table is defined as utf-8_general_ci
  4. 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

Aris
  • 4,643
  • 1
  • 41
  • 38
1

what finally helped me is to add the charset to the connection:

{"mysql:host=$host;dbname=$db;charset=utf8"}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Holy Moses
  • 65
  • 7
0

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 :)

peterpeterson
  • 1,315
  • 2
  • 14
  • 38
0

For future use, if you have this issue and you are using PDO and not mysqli, you will need to do it like this:

  1. The Database (all of it) collation has to be utf8_general_ci.
  2. Set the collation of the table with Hebrew also to utf8_general_ci.
  3. In the HTML "head" tag, add this: < meta charset="utf-8" >
  4. In your PHP connection file, add after the connection Query this line: conn->exec("set names utf8");
  5. 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.

GabMic
  • 1,422
  • 1
  • 20
  • 37
-1

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));
}
Dr. Avri
  • 11
  • 4