简体   繁体   中英

Turkish character issue while inserting into MySQL database with PHP

I have trouble with inserting strings that includes Turkish characters. I am trying to insert "öçşığüÖÇŞİĞÜ" as string.

I put the line indicated below into the head of php file:

header("Content-Type: text/html; charset=windows-1254");

I get data from ajax with (although it is not important, I write it)

$json = filter_input_array(INPUT_POST);

Here is insert command:

$query = "INSERT INTO users (name) VALUES ('" . $user->get_name() . "')";

Now, I tried two cases of assigning values to variables:

CASE 1

  1. php: $name = $json['name'];
  2. browser: öçşığüÖÇÅÄ°ÄÃœ
  3. database: çşığüÖÇŞİĞÜ

CASE 2

  1. php: $name = iconv("UTF-8", "ISO-8859-9", $json['name']);
  2. browser: öçşığüÖÇŞİĞÜ
  3. database: çþýðüÖÇÞÝÐÜ

"1" is my assigning type of string, "2" is the result of "echo $user->get_name();" depending on the assignment at "1", and "3" is the value in database cell. I can get true result in "echo" in CASE 2, but corrupted result is inserted into database. My DB collation is: "utf8_turkish_ci".

(Php 5.4.17, MySQL 5.6.13)

this problem sounds like you've missed to specify a character encoding somewhere. to solve this, simply make sure you've set character encoding to utf-8 everywere (it doesn't actually need to be utf-8, just the same everywhere - but if you've messed up something and need to change some places anyway, i'd strongly recommend using utf-8):

  • tell MySQL to use utf-8. to do this, add this to your my.cnf:

     collation_server = utf8_unicode_ci character_set_server = utf8 
  • before interacting with mysql, send this two querys:

     SET NAMES 'utf8'; CHARSET 'utf8'; 

    or, alternatively, let php do this afteropening the connection:

     mysql_set_charset('utf8', $conn); 
  • set UTF-8 as the default charset for your database

     CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8'; 
  • do the same for tables:

     CREATE TABLE `my_table` ( -- ... ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
  • assuming the client is a browser, serve your content as utf-8 and the the correct header:

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

    to be really sure the browser understands, add a meta-tag:

     <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
  • and, last but not least, tell the browser to submit forms using utf-8

     <form accept-charset="utf-8" ...> 

Or check here smiler question . MYSQL - Turkish character

If you use utf-8 this problem doesn't occur.

My suggestion is convert encoding to utf8.

I think you used utf 8, please try

header('Content-Type: text/html; charset=utf-8');

in mysql part you must use

mysql_query("set names 'utf8'");

example php

<html>
    <head>
        <title>Title</title>
        <meta charset="UTF-8" />   
    </head>
    <body>
    <?php
            mysql_query("set names 'utf8'");   
            $sql = "select name from users";
            //........
            //........
            //echo "abc";           
    ?>    

    </body>
<html>

You should add the MySQL query before any queries to this table:

SET NAMES 'utf8_turkish_ci' / SET NAMES 'ISO-8859-9'

or

$params = [
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'ISO-8859-9\'',
];
$pdo = new PDO($dsn, $username, $passwd, $params];

Don't use iconv for that.

I solved by only

  • removing iconv() conversions
  • replacing previous header with "header('Content-Type: text/html; charset=utf-8');" as indicated above by other members.

thanks a lot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM