1

I am trying to insert unicode content on phpmyadmin in xampp. I set both the collation for my database and for my table to utf8. But whenever I am trying to insert the unicode data(i.e. Nepali or hindi fonts), the data is stored like this: The highlighted text.

The data is displayed correctly. As I have set charset on meta tag to utf-8. The main problem is I want to store the data as it is.

Is there any way to store the data as it is? The data actually is: "समाचार".

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
Sandesh
  • 29
  • 3
  • Show you code....... – Vinod VT Aug 03 '16 at 05:30
  • INSERT INTO [table_name] SET added_date = now(), added_by = 1, category_name = 'खेलकुद', category_status = '1', show_in_menu = '1' This is my insert code. I am using mysqli. – Sandesh Aug 03 '16 at 05:34
  • Try going into phpMyAdmin and clicking the structure tab for the table then find the "collation" dropdown box for the field you'd like to store the utf-8 characters in, then change it to utf8_unicode_ci. Then try inserting the data. – Ultimater Aug 03 '16 at 05:35
  • @Ultimater I already did that as well. Both the collation to utf8_unicode_ci as well. But did not work. – Sandesh Aug 03 '16 at 05:37
  • Works for me, here's a SQL dump: http://pastie.org/10927743 – Ultimater Aug 03 '16 at 05:41
  • @Ultimater: It works only when you insert it manually from phpmyadmin. Try it using php. And use mysqli. It will display "??????" – Sandesh Aug 03 '16 at 05:47
  • If you're inserting it from PHP, you need to set the correct database adapter settings. Are you using PDO or MySQLi? (ah just noticed you said mysqli) – Ultimater Aug 03 '16 at 05:48
  • @Ultimater I am using MySQLi. – Sandesh Aug 03 '16 at 05:51
  • So, is it Mojibake? Or question marks? They are caused by different things. Both are covered [_here_](http://stackoverflow.com/a/38363567/1766831). – Rick James Aug 03 '16 at 19:41
  • Its actually question marks. – Sandesh Aug 04 '16 at 06:36

1 Answers1

1

You need to set the charset on the adapter so it doesn't corrupt the characters before trying to insert them. Since you're using MySQLi as your database adapter, you'd be looking for one of the following solutions:

// Procedural style
mysqli_set_charset($link, "utf8");

// Object oriented style
$mysqli->set_charset("utf8");

See the manual entry for set_charset here: http://php.net/manual/en/mysqli.set-charset.php


You can also run an initial query SET NAMES utf8.
Ultimater
  • 4,647
  • 2
  • 29
  • 43
  • 1
    Yes its the correct way. Or you can do something like this: $connectionVar->query("SET NAMES urf8"); – Sandesh Aug 03 '16 at 06:06