can't insert russian text into mysql database

22,361

Solution 1

Try calling mysql_set_charset('utf8'); after connecting to the database. I think it's similar to executing a SET NAMES query, but since the PHP manual says using that function over a SET NAMES query is recommended, I'd try it.

Also, when you display your content, you could try echo htmlentities($string, ENT_COMPAT, 'UTF-8');

Solution 2

Check your MySQL configuration and ensure that your encoding is defined correctly. Add these lines to my.cnf or my.ini, which ever your installation uses. These settings did the trick for me:

[client]
default-character-set=utf8


[mysql]
default-character-set=utf8


[mysqld]
character-set-server=utf8

Solution 3

I have tried multiple collations in phpMyAdmin as well as changing the charset of the page, which didn;t make sense but i was willing to try anything after two days of research. this command helped me: mysql_set_charset('utf8');

Collation on the column was set to koi8r_general_ci

Share:
22,361
yozhik
Author by

yozhik

Updated on May 12, 2020

Comments

  • yozhik
    yozhik almost 4 years

    When I'm trying to insert russian text into MySQL database it inserts it like: г???????????? ?? ????????
    Рісѓрїр°ріс‹рї р° с‹рір°рї

    So, I have two pages: registration.php and addUser.php. In each of them

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    

    Database consist of 11 tables, each table has collation: utf8_general_ci, type: MyISAM. Each field in every table has Collation: utf8_general_ci.

    When I'm writing to database directly in phpMyAdmin and then show this data to web-page. In English and Russian - all OK.

    But when I'm full my form with personal data on registration.php and then going to addUser.php - all russian characters displayed like I wrote upper - on page and in database too.

        function AddNewUser($Name, $Surname, $FatherName, $Email, $Password, $Phone, $DegreeID, $RankID, 
    $Organization, $Department, $Country, $City, $Address, $Job)
    {
            //fetch data from database for dropdown lists
            //connect to db or die)
        $db = mysql_connect($GLOBALS["gl_kussdbName"], $GLOBALS["gl_kussUserName"], $GLOBALS["gl_kussPassword"] ) or die ("Unable to connect");
    
        //to prevenr ????? symbols in unicode - utf-8 coding
        mysql_query("SET NAMES 'UTF8'");
    
        //select database
        mysql_select_db($GLOBALS["gl_kussDatabase"], $db);
        $sql = "INSERT INTO UserDetails (
    UserFirstName,
    UserLastName,
    UserFatherName,
    UserEmail,
    UserPassword,
    UserPhone,
    UserAcadDegreeID,
    UserAcadRankID,
    UserOrganization,
    UserDepartment,
    UserCountry,
    UserCity,
    UserAddress,
    UserPosition) 
    VALUES(
    '".$Name."',
    '".$Surname."',
    '".$FatherName."',
    '".$Email."',
    '".$Password."',
    '".$Phone."',
    '".$DegreeID."',
    '".$RankID."',
    '".$Organization."',
    '".$Department."',
    '".$Country."',
    '".$City."',
    '".$Address."',
    '".$Job."'
    );";
        //execute SQL-query
        $result = mysql_query($sql, $db);
        if (!$result) 
        {
            die('Invalid query: ' . mysql_error());
        }
        //close database  = very inportant
        mysql_close($db);
    
    }
    ?>
    

    There also such information in phpMyAdmin:

    auto increment increment    1
    auto increment offset   1
    autocommit  ON
    automatic sp privileges ON
    back log    50
    basedir \usr\local\mysql-5.1\
    big tables  OFF
    binlog cache size   32,768
    binlog format   STATEMENT
    bulk insert buffer size 8,388,608
    character set client    utf8
    (Global value)  cp1251
    character set connection    utf8
    (Global value)  cp1251
    character set database  cp1251
    character set filesystem    binary
    character set results   utf8
    (Global value)  cp1251
    character set server    cp1251
    character set system    utf8
    character sets dir  \usr\local\mysql-5.1\share\charsets\
    collation connection    utf8_general_ci
    (Global value)  cp1251_general_ci
    collation database  cp1251_general_ci
    collation server    cp1251_general_ci
    completion type 0
    concurrent insert   1
    

    So I need to properly show, save and select russian text from database. Thanx! connect timeout 10 datadir \usr\local\mysql-5.1\data\

  • yozhik
    yozhik over 13 years
    I'm using mysql_set_charset('utf8');
  • Avnish alok
    Avnish alok almost 8 years
    @yozhik change the table field to accept UTF-8. And on the result page where you want to show the DB info. set the UTF-8 for mysqli query using mysqli_query($con,"SET NAMES utf8");