0

I am trying to insert some names in mysql database by using mysqli commands. The problem is whenever the Turkish character ü comes in text, the text in the database becomes just previous part of that character.

For example I try to insert : Humana Still Tee Anne Sütünü Arttıran Bitki Çayı

The text in database : Humana Still Tee Anne S

The column in database is a text column has utf-8 general_ci standards.

And these are my codes for inserting

 function mysqli_connector()
{
  $link = mysqli_connect(DB_HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_DATABASE) or    die("hata");
  $link->set_charset("utf8");
  mysqli_set_charset($link,"utf8");
  return $link;
}

function oc_product_description($p_id,$name){
  $link = mysqli_connector();
  $link->query("insert into oc_product_description (product_id,name,language_id,meta_title)values('$p_id','$name','1','$name')")or die(mysqli_error($link));
  mysqli_close($link);
}


oc_product_description(106,"Humana Still Tee Anne Sütünü Arttıran Bitki Çayı");

The problem occurs for ü letter i tried with other Turkish characters, it works fine.

Thanks for your time.

Ali insan Soyaslan
  • 836
  • 5
  • 14
  • 33
  • Try doing html encoding for inserting and decoding for reading maybe ? – Maximus2012 May 21 '15 at 21:47
  • see if this helps: http://stackoverflow.com/questions/1873793/html-encode-in-php – Maximus2012 May 21 '15 at 21:47
  • What do you mean by html encoding ? Just writing the names in html tags? – Ali insan Soyaslan May 21 '15 at 21:48
  • @Maximus2012 No, that's completely wrong. HTML encoding is not the context being used here, it is SQL, and should be encoded for SQL (unless prepared statements are being used). – Sven May 21 '15 at 21:48
  • Not exactly. Please take a look at the link I added and also the manual page for that and related functions. I am not entirely sure if it will work for your case though. – Maximus2012 May 21 '15 at 21:49
  • @Sven I think you're right but you think prepared statements will work for this case ? – Maximus2012 May 21 '15 at 21:49
  • Prepared statements or escaped strings written into the SQL (that's what is missing currently!) – Sven May 21 '15 at 21:50
  • Try wrapping your text inputs in `mysqli_real_escape_string` calls. This is probably a good idea, generally. But it should encode the string so that your database can handle it. Also, make sure that your varchar field is long enough to store the string. – Martin Sheeks May 21 '15 at 21:50
  • Can you try `oc_product_description(106,"Humana Still Tee Anne S\xC3\xBCt\xC3\xBCn\xC3\xBC Arttıran Bitki");`? – Federkun May 21 '15 at 22:05
  • @Leggendario I tried but it did not work :( – Ali insan Soyaslan May 21 '15 at 22:15
  • How have you checked that the text is truncate? phpmyadmin? Can you give us more details on the table? What version of MySQL are you using? – Federkun May 21 '15 at 22:24

2 Answers2

0

Characters Truncated Using Utf8 is usually caused by

  • The client had accented characters encoded in latin1, and
  • SET NAMES utf8 was in effect during the INSERT

It does not matter what the CHARACTER SET is on the table and column; the problem occurs during the transfer to the server.

For example, here were the steps for INSERTing é:

  • The client had the é encoded as hex E9, the latin1 encoding.
  • When transferring to the server, SET NAMES utf8 said (incorrectly) "The bytes in the client are encoded as utf8".
  • But there is no utf8 character encoded E9, so the string is truncated before the E9

That is, the string is stored up to, but not including, the invalid encoding.

I have talked about utf8 and latin1, but many (not all) pairs of character sets suffer from this "truncation" problem.

The data cannot be recovered from the table.

Going forward, the fix is to have these agree:

  • the encoding of the characters in the client
  • the declared encoding in SET NAMES (or equivalent)

Note that it does not matter what the target column's CHARACTER SET is; the purpose of SET NAMES is to convert between what the client has and what the column has. However, it is generally good to use utf8mb4 (or at least utf8) for all text.

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

Change your data type of fieldname to "varchar" and give the size.... Then your field will looks like this "varchar(300)"