6

I'm trying to store an Arabic text to the table, I searched a lot but I didn't find a solution that worked for me, so this is what I got:

$en = "OK";
$ar = "حسناً";
$link->query("INSERT INTO words (en,ar) VALUES ($en,$ar)");

The problem is when I insert it, the Arabic text looks like حسناً, my table's collation and MySQL's are utf8_general_ci, so is my database's, I also have mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');, but it doesn't work.

Jasper
  • 11,590
  • 6
  • 38
  • 55
PepsiGam3r
  • 302
  • 1
  • 4
  • 10

3 Answers3

12

I recently had the same issues myself.

Here's a few pointers:

  • ALL attributes must be set to ut8 (collation is NOT the same as charset)
  • Save the document as UTF-8 (If you're using Notepad++, it's Format -> Convert to UFT-8)
  • The header in both PHP and HTML should be set to UTF-8 (HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and PHP: header('Content-Type: text/html; charset=utf-8');
  • Upon connecting to the databse, set the charset ti UTF-8 there as well, like this: $link->set_charset("utf8"); (directly after connecting)
  • Also make sure your database and tables are set to UTF-8, you can do that like this:

ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Remember that EVERYTHING needs to be set to UFT-8 charcode, or else it'll insert stuff like "حسناً". Hope this helped!

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 1
    `header('Content-Type: text/html; charset=utf-8');` has fixed my problem, thanks. – PepsiGam3r Jul 16 '15 at 16:24
  • Happy to be of assistance! Don't forget to tag the answer as correct if it helped :) – Qirel Jul 16 '15 at 16:25
  • Is there anything I can do to change the text in table column data, I already entered data from another table? – krachleur May 21 '20 at 13:22
  • 1
    If the data is already inserted with a broken encoding, its not easy to fix it. The best way is to re-insert or update the data. There are some tools that has been made that forces UTF8, but none that can be relied on 100%. – Qirel May 21 '20 at 13:36
  • Thanx a lot @Qire – krachleur May 21 '20 at 13:38
4

First thing is the database, table, and column. See if utf8 is set: utf8_general_ci or utf8_unicode_ci

Also, the connection collation: utf8_general_ci

In PHP, after I connect with mysqli, I issue this:

$conn->query("SET NAMES 'utf8'");

And the web page output always jams in:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Drew
  • 24,851
  • 10
  • 43
  • 78
0

Here is a solution that worked for me:

  1. when you create the database choose the collation for: utf8_unicode_ci

  2. when you connect to the database from php use the following character set:

    $dsn = "mysql:host=localhost;dbname=record;charset=utf8";
    
Paul Roub
  • 36,322
  • 27
  • 84
  • 93