Php mysql SET NAMES 'utf8' COLLATE 'utf8_unicode_ci' does not work with mysqli

I am migrating my site to php mysqli from php methods mysql_ *.

I had the following code that completed the task:

mysql_query ("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");

Without this request, my string characters (in Georgian) were written with question marks. For example, it was written ????????? instead of გამარჯობა

Since he did his work, I was happy, but now I can not do the same with mysqli.

$mysqli = new mysqli("localhost", "root", "", "test"); $mysqli->query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'"); 

Can anyone help me out? Thanks.

+4
source share
5 answers

It is not recommended to use a mysqli query to set names, but mysqli :: set_charset

 $mysqli = new mysqli("localhost", "root", "", "test"); $mysqli->set_charset("utf8"); 
+16
source

You can use mysqli_set_charset

This is the preferred way to change the encoding. Using mysqli_query () to set it (e.g. SET NAMES utf8) is not recommended.

However, to set up the mapping, you still have to use the SET NAMES query.

+1
source

http://php.net/manual/en/mysqli.set-charset.php

or

mysqli->set_charset("utf8")

+1
source

A request / error report has been sent for a PHP function ...

See https://bugs.php.net/bug.php?id=52267 , which received a response from uw@php.net :

... use mysqli_set_charset() to set the encoding, and then SET NAMES to change the sorting.

It also refers to http://dev.mysql.com/doc/refman/5.1/en/mysql-set-character-set.html

This function is used to set the default character set for the current connection. The string csname indicates a valid character set name. Compound matching becomes the default character set sort. This function works like the SET NAMES statement, but also sets the value mysql->charset and thus affects the character set used by mysql_real_escape_string()

And I will contact http://dev.mysql.com/doc/refman/5.6/en/charset-collate.html , which shows how to make requests using any sort that satisfies this request.

With the COLLATE clause, you can override all default mappings to compare. COLLATE can be used in various parts of SQL statements. Here are some examples:

  • With ORDER BY :
    SELECT k FROM t1 ORDER BY k COLLATE latin1_german2_ci;

  • With AS :
    SELECT k COLLATE latin1_german2_ci AS k1 FROM t1 ORDER BY k1;

  • With GROUP BY :
    SELECT k FROM t1 GROUP BY k COLLATE latin1_german2_ci;

  • With cumulative features:
    SELECT MAX(k COLLATE latin1_german2_ci) FROM t1;

  • With DISTINCT :
    SELECT DISTINCT k COLLATE latin1_german2_ci FROM t1;

  • With WHERE :
    SELECT * FROM t1 WHERE _latin1 'Müller' COLLATE latin1_german2_ci = k; SELECT * FROM t1 WHERE k LIKE _latin1 'Müller' COLLATE latin1_german2_ci;

  • With HAVING :
    SELECT k FROM t1 GROUP BY k HAVING k = _latin1 'Müller' COLLATE latin1_german2_ci;

0
source
 $con=mysqli_connect("localhost","admin","1234","web"); $con->set_charset("utf8"); 
-3
source

Source: https://habr.com/ru/post/1446681/


All Articles