Reputation: 1392
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
CASE 2
"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)
Upvotes: 0
Views: 6915
Reputation: 300
If you use utf-8 this problem doesn't occur.
My suggestion is convert encoding to utf8.
Upvotes: 2
Reputation: 1392
I solved by only
thanks a lot.
Upvotes: 0
Reputation: 864
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.
Upvotes: 1
Reputation: 1150
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>
Upvotes: 1
Reputation: 1062
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
Upvotes: 3