Faced the problem of sampling data from the database when using the query with Russian letters.

There is a test database in the utf8_general_ci encoding, in it the table is also in the utf8_general_ci encoding. In table 2, the columns are eng and rus in which the data are:

eng rus GGGG ГГГГГ GGGG ГГГГ 

and there is php code

 <?php $dbname = 'test'; $link = mysql_connect('localhost', 'root', 'password'); mysql_select_db($dbname,$link); if (!$link) { echo 'Ошибка подключения к mysql'; exit; } $sql = "SELECT count(*) from `test` WHERE `eng` like 'ГГ%'"; $res = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_row($res); $k_el = $row[0]; echo $k_el; ?> 

When using sql query

 SELECT count(*) from `test` WHERE `eng` like 'ГГ%' 

Gives out: 0
When using the query

 SELECT count(*) from `test` WHERE `eng` like 'GG%' 

Throws 2, as expected.

Those. There is no sampling on Russian letters from a DB.

If you make a database and tables in cp_1251_general_ci, then it gives an error

Illegal mix of collations (cp1251_general_ci, IMPLICIT) and
(latin1_swedish_ci, COERCIBLE) for operation 'like'

The problem is in the encoding, but how to fix it?

    2 answers 2

    After

     mysql_select_db($dbname,$link); 

    add line

      mysql_set_charset("utf8"); 
    • Earned, thank you very much. - alogin
    • do not forget to mark as the correct answer - HELO WORD

    Why did you decide that the problem is in the encoding? You are looking, judging by the code, only in the eng column, and there is no GG there - that's right.

    Try to execute the query SELECT count(*) from test WHERE rus like 'ГГ%' For sure, 2 lines will be returned, as it should.

    Well, I recommend reading the documentation to understand what is collation in general and what it affects.

    • It was simply sealed, in the end it still returns 0. Helped the comment above, where you need to specify mysql_set_charset ("utf8"); - alogin