Vikram Anand Bhushan
Vikram Anand Bhushan

Reputation: 4886

UTF 8 encoding not working properly in PHP

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

Upvotes: 4

Views: 4686

Answers (2)

Iftikhar Khan
Iftikhar Khan

Reputation: 313

I've used bellow function and its working for me

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

Upvotes: 0

GoBusto
GoBusto

Reputation: 4778

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">

Upvotes: 3

Related Questions