How to insert an hebrew value into a mysql db in php
Solution 1
Check the collation_connection:
show variables like '%collation%'
Solution 2
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
Solution 3
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 />";}
Solution 4
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
Imri Persiado
Updated on June 14, 2022Comments
-
Imri Persiado almost 2 years
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 almost 11 yearsI'm sorry, I didn't understand your answer. can you add some more details? Thanks.
-
dwjv almost 11 yearsYou 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.
-
yuvalsab over 9 yearsThat was helpful. Thanks!
-
Itai Spector over 8 yearsSardar Raj this was the magic spot for me: mysql_query("SET NAMES 'utf8'") +1