Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert an hebrew value into a mysql db in php

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.

like image 891
Imri Persiado Avatar asked May 30 '13 12:05

Imri Persiado


2 Answers

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

like image 126
Sarkar Raj Avatar answered Nov 15 '22 16:11

Sarkar Raj


Check the collation_connection:

show variables like '%collation%'
like image 35
dwjv Avatar answered Nov 15 '22 15:11

dwjv