简体   繁体   中英

php and mysql database turkish character

Hello friends i know this question has in stackoverflow but i search all question answer but not solition for me. My problem is mysql and php turkish character problem.

I am using this this <meta>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

But turkish character looking this : üŠüdü

I try db connection like this:

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'test');
mysql_query("SET NAMES UTF8");
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

but turkish character problem continued.

my phpmyadmin 3 tables sum collation: utf8_general_ci

message table collation: latin1_swedish_ci

<?php
$Wall = new Wall_Updates();
if(isSet($_POST['update']))
{
$update=mysql_real_escape_string($_POST['update']);
$data=$Wall->Insert_Update($id,$update);

if($data)
{
$msg_id=$data['msg_id'];
$message=tolink(htmlentities($data['message']));
$time=$data['created'];
$id=$data['uid_fk'];
$name=$data['name'];
$face=$Wall->Gravatar($id);
//$commentsarray=$Wall->Comments($msg_id);
?>

In this way screenshot link click How can I solve the problem of Turkish character.

$.ajax({
type: "POST",
url: "comment_ajax.php",
data: dataString,
cache: false,
success: function(html){
$("#commentload"+ID).append(html);
$("#ctextarea"+ID).val('');
$("#ctextarea"+ID).focus();
 }
 });
}
return false;
});

Can you try

  mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD);
  @mysql_select_db(DB_DATABASE) or die( "Unable to select database");
  mysql_set_charset('utf8');

Working ok with greek for me

HTML Entities:

Judging by your screenshot , the database contains HTML encoded entities which means that sometime before saving your data in the DB you are altering it by applying htmlentities over it.

Unfortunately if your PHP version is < 5.4 the optional parameter $encoding does not default to UTF-8 so it treats your string like ASCII.

Multi-byte vs. single-byte:

Because of this multi-byte characters are broken into single-byte characters and HTML encoded before being saved.

When retrieving the data from the database you HTML source code will have HTML entities (ex.: &Atilde; ) but your browser will render them the way you're seeing them.

Not using htmlentities should fix your problem and is recommended or you could try using html_entity_decode before outputting the data (not sure if it won't cause more trouble than it fixes).

How to check for HTML entities:

View the HTML source code of the site and check if there are any entities in the text fetched from the DB (ex.: &Atilde; ).

Change this :

<?php
$Wall = new Wall_Updates();
if(isSet($_POST['update']))
{
$update=mysql_real_escape_string($_POST['update']);
$data=$Wall->Insert_Update($id,$update);

if($data)
{
....
?>

to this :

<?php
$Wall = new Wall_Updates();
if(isSet($_POST['update']))
{
$update=html_entity_decode(mysql_real_escape_string($_POST['update']));
$data=$Wall->Insert_Update($id,$update);

if($data) 
{
....
?>

It is better for you to change your table's encoding to utf8 and connect to mysql like this:

$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
mysql_query('set names "utf8" collate "utf8_turkish_ci" ');

Put this line below $database variable line and Try

mysql_set_charset('utf8',$database);

and while inserting this kind of data, you have to use N along with your value in your query N('$yourvalue')

I had same problem and solved it like that

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "tamirat";

$co = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($co, 'utf8');

Just write the connection stuff first and write 'utf8'. I hope this will help.

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