3

I am trying to print data from MySql database.

I have a simple problem it is showing D�lar instead of Dólar .

Although I have included

<META http-equiv="Content-Type" Content="text/html; charset=utf-8">

In my html page so can any one help me out with this

Thanks in Advance

GoBusto
  • 4,632
  • 6
  • 28
  • 45
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130
  • 1
    problem may be that your database has another charset for taht database / table – Alexis Peters Feb 26 '15 at 09:15
  • UTF 8 encoding not working properly in *your* PHP *script*... Because UTF-8 works perfectly in PHP, if you use it correctly. – STT LCU Feb 26 '15 at 10:17
  • in case of ajax response you have to set in content-type var jax = createAjax(); jax.open("POST",path,true) jax.setRequestHeader('Content-type','text/html; charset=utf-8'); – Pankaj katiyar Feb 26 '15 at 10:20

2 Answers2

3

The character set needs to be defined in a few different places:

The MySQL database

The text stored in the database might not be encoded as UTF-8. You can define a default character set as part of the create database statement:

CREATE DATABASE mydb CHARACTER SET utf8;

You can also specify per-column character sets with create table.

Within your PHP code

You'll need to tell your client-side code which encoding it should use when communicating with the database.

If you're using PDO, you can specify the character set as part of the DSN string:

$dsn = 'mysql:host=localhost;dbname=testdb;charset=utf8';
$dbh = new PDO($dsn, $username, $password);

If you're using MySQLi, you can use the mysqli_set_charset() function/method:

$dbh->set_charset('utf8');

or:

mysqli_set_charset($dbh, 'utf8');

Alternatively, the MySQL website suggests issuing a statement after connecting to the server:

SET NAMES 'utf8';

Within the HTML output

For HTML5, you can simply add the following <meta> tag within the <head> element of your output:

<meta charset="utf-8">
GoBusto
  • 4,632
  • 6
  • 28
  • 45
  • so with the statement issuing a statement is like I will execute SET NAMES 'utf8' ; ?? – Vikram Anand Bhushan Feb 26 '15 at 10:56
  • Yes, just run `SET NAMES 'utf8';` as an SQL statement once you've connected to the database, in the same way as you would run `SELECT * FROM x` or similar. I'd personally suggest using one of the other methods, though. **EDIT:** [This answer](http://stackoverflow.com/a/4361485/4200092) might be useful. – GoBusto Feb 26 '15 at 11:01
  • 1
    thanks bro Yeah I tried this but its not working , unluckily okay I will use the other method . – Vikram Anand Bhushan Feb 26 '15 at 11:02
0

I've used bellow function and its working for me

call_user_func_array('mb_convert_encoding', array(&$str,'HTML-ENTITIES','UTF-8'));

Iftikhar Khan
  • 313
  • 1
  • 5
  • 9