0

I am using tamil language (utf-8) inserted into MySQL database collation latin1_swedish_ci by default. but the data shows like ????? ??? ???????????.????? when I retrieve it. I studied in the net lot about the problem. but the solution nothing was useful. Totally it is making mad. anybody can help me. I am using query below like this. Give me solution in simple way only.

<?php

$con=mysql_connect('localhost','root','');

$db=mysql_select_db('nikah', $con);

$sql="select * from matrimony";
$result=mysql_query($sql) or die(mysql_error());
While($row=mysql_fetch_array($result)){
     echo $row['name'];
     }

?>
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 13 '16 at 18:23

2 Answers2

1

Try using mysql_set_charset() as explained here.

$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);

EDIT: As stated by Jay keep in mind that mysql extension has been deprecated in PHP 5.5.0. From the php doc:

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_set_charset() PDO: Add charset to the connection string, such as charset=utf8

marian0
  • 664
  • 6
  • 15
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 13 '16 at 18:23
0

In addition to setting mysql_set_charset("utf8"); as mentioned in the other answer, you might need to adjust for a few more settings in order to fully guard yourself against broken characters.

Connection

The connection needs to know what charset to expect. Just after creating the connection, specify the charset like this

$con = mysql_connect('localhost','root','');
mysql_set_charset("utf8");

Headers

Setting the charset in both HTML and PHP headers to UTF-8

  • PHP: header('Content-Type: text/html; charset=utf-8');
    (PHP headers has to be placed before any kind output (echo, whitespace, HTML))

  • HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    (HTML-headers are placed within the <head> / </head> tag)

Database and tables

Your database and all its tables has to be set to UTF-8. Note that charset is not exactly the same as collation (see this post).

You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)

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

Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.


mysql_* functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you shoud stop using them if you can. You should choose another API, like mysqli_* or PDO instead - see choosing an API.

Community
  • 1
  • 1
Qirel
  • 25,449
  • 7
  • 45
  • 62
  • I done every thing. Now I am getting result like this. விவாகரதà¯à®¤à¯ ஆனவர௠but my language like this உதவிக்கு நன்றி – yaseen ahmed Jan 13 '16 at 19:41
  • What does the characters look like when you look at them in the database? – Qirel Jan 13 '16 at 19:47
  • it is look like this in database :விவாகரதà¯à®¤à¯ ஆனவரà¯after change all column manualy to utf8 it shows உதவிக்கு நன்றி like this original font but once take backup and restore again it comes :விவாகரதà¯à®¤à like this but now not possiple to change column to utf8 because all column already utf8 – yaseen ahmed Jan 13 '16 at 20:03
  • If they are stored incorrectly from before you made these changes, you'll need to update them with new values that has the correct charset. The old values in the database had the wrong encoding, and thus will *always* have the wrong encoding. You should insert new values with UTF-8 charset. – Qirel Jan 13 '16 at 20:08
  • ok sir. but what todo with the old data lot. – yaseen ahmed Jan 13 '16 at 20:20
  • You can use something like [ForceUTF8](https://github.com/neitanod/forceutf8) and loop through your data, converting it to UTF-8. – Qirel Jan 13 '16 at 20:20
  • how to use this code? – yaseen ahmed Jan 13 '16 at 21:08
  • Read the documentation. Basically, you'll need to loop through everything in your database (query a select statement), and update the values with the function given in that link. – Qirel Jan 13 '16 at 23:51
  • almost my problem is over. but in internet explorer in some version having problem. – yaseen ahmed Apr 10 '16 at 17:00