4

I can't understand whats problem. I write data to mysql with php and utf-8 issues

Write in HTML form Binəqədi r. Çiçək qəs

Writed database Bin?q?di r. ÃÃÂ

When I use mysqli_set_charset($conn,"utf8"); in insert and update query

Writed database Bin?q?di r. Çiç?k q?s

Write data manually direct to database and use mysqli_set_charset($conn,"utf8"); in select works normal.

How can I fix it? INSERT and UPDATE with UTF-8. This is my insert code

public function insert($sql) {
    $conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    mysqli_set_charset($conn,"utf8");
//  $conn->set_charset("utf8"); use same this.
    if ($conn->query($sql) === TRUE) {
        return 1;
    } else {
        return 0;
    }
    $conn->close();
}

NOTE: This issues on Linux and OSX. In windows work goods. How can I fix it for linux hosting?

Farid Valiyev
  • 203
  • 5
  • 20
  • I always use `$conn->query("SET CHARSET UTF8");` and I have never got these problems, also check if in HTML you are using `` – Guilherme Lopes Jan 20 '16 at 17:05
  • And do check if the collation of your database/table/field is any of the utf8 variations. And do check if the html where the form resides is utf8. – frz3993 Jan 20 '16 at 17:09
  • @GuilhermeLopes İ already try this. – Farid Valiyev Jan 20 '16 at 17:14
  • @frz3993 mysql charset is utf8_bin. in html head – Farid Valiyev Jan 20 '16 at 17:15
  • It looks like there are _two_ things going wrong. Please provide `SELECT col, HEX(col) FROM ...` so we can see the hex that got stored. From there, I may be able to deduce what happened. – Rick James Jan 25 '16 at 23:48
  • @RickJames same code worked on Windows 10, but doesn't work Mac OSX and Linux Hosting. How can i trust configure MySQL on Linux Hosting? – Farid Valiyev Jan 26 '16 at 12:53
  • 2
    There are _four_ places you need to configure to get character sets to work. (1) the bytes in the client, (2) the connection to MySQL, (3) declaring `CHARACTER SET utf8`, (4) the meta tag in html. Run the select so I can see more. – Rick James Jan 26 '16 at 18:36
  • If your mysql charset is utf8_bin then the field must be VARBINARY not VARCHAR. – Christophe Maggi Apr 25 '16 at 13:31

5 Answers5

5

try

AddDefaultCharset utf-8

in you apache configuration

or in you php file use

header( 'content-type: text/html; charset=utf-8' );

and also try to

default_charset = "utf-8";

in you php.ini

also you can try with

  [client]
  default-character-set=utf8

  [mysql]
  default-character-set=utf8

  [mysqld]
  character-set-client-handshake = false #force encoding to uft8
  character-set-server=utf8
  collation-server=utf8_general_ci

  [mysqld_safe]
  default-character-set=utf8

in your my.ini and restart mysql demon

Rick James
  • 135,179
  • 13
  • 127
  • 222
bns
  • 392
  • 2
  • 9
  • How can I change apache configuration in GoDaddy Hosting.? and in cPanel I can't find php.ini file. How can I find? – Farid Valiyev Apr 19 '16 at 07:35
  • i think you can create your own php.ini file and the upload it via ftp to your godaddy account. For the apache configuration I am not sure, but I bet you can create a ticket asking whats the current configuration or how you change it. – bns Apr 20 '16 at 15:36
  • 1
    I corrected the spelling of `utf8` for MySQL. However, that may not be sufficient; `default-character-set` was changed to `character-set-server` 6 years ago in 5.5.3. – Rick James Apr 24 '16 at 22:03
1

Please consider all of these:

  1. Before your queries, do this:

    $conn->query('SET NAMES utf8');
    
  2. Be sure your file encoding is utf8 (not the html encoding). I mean the file encoding which your IDE is using.

  3. Be sure about your table charset

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
0

This is clearly a duplicate of a well-known issue, but cannot be flagged for closure due to the bounty. So, the checklist:

  1. Ensure database columns, tables, and database are set to character set utf8mb4
  2. Ensure the connection is set to the same character set with $conn->set_charset("utf8mb4");
  3. Ensure your server is configured to inform web browsers that it will be sending Unicode text. In PHP, do header("Content-Type: text/html;charset=utf8"); or set it up in your web server configuration.
Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154
0

Try using set names

public function insert($sql) {
    $conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
   // $conn->set_charset("utf8");
    $conn->query("SET NAMES 'utf8'");
    if ($conn->query($sql) === TRUE) {
        return 1;
    } else {
        return 0;
    }
    $conn->close();
}

From MySQL (http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html):

  • SET NAMES 'charset_name' [COLLATE 'collation_name']

SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)

A SET NAMES 'charset_name' statement is equivalent to these three statements:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;

Setting character_set_connection to charset_name also implicitly sets collation_connection to the default collation for charset_name. It is unnecessary to set that collation explicitly. To specify a particular collation, use the optional COLLATE clause:

  • SET CHARACTER SET charset_name

SET CHARACTER SET is similar to SET NAMES but sets character_set_connection and collation_connection to character_set_database and collation_database. A SET CHARACTER SET charset_name statement is equivalent to these three statements:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET collation_connection = @@collation_database;

Setting collation_connection also implicitly sets character_set_connection to the character set associated with the collation (equivalent to executing SET character_set_connection = @@character_set_database). It is unnecessary to set character_set_connection explicitly.

My opinion is that in some point the proper character set for the field description is not specified when using stored procedure. Do check again that your database/table/field have as character set utf8_general_ci

From MySQL (http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html):

Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy if there are characters that are not in both character sets.

0

The gibberish you displayed seems to involve more than one error. Çiç, when Mojibaked to latin1 gives Çiç.

Please provide SELECT col, HEX(col) FROM ... for something that is 'bad'.

Meanwhile, your reply about the stored procedure -- Do SHOW CREATE PROCEDURE ... and check what character set was in effect when the procedure was created. That could be part of the problem. (I noticed that that decade-old bug report you mentioned failed to consider this aspect.)

Rick James
  • 135,179
  • 13
  • 127
  • 222