0

So I'm trying to find a fast way to show all my results from my database, but I can't seem to figure out why I need to add the utf8_encode() function to all of my text in order to show all my characters properly.

For the record, my database information is both French and English, so I will need special characters including à, ç, è, é, ê, î, ö, ô, ù (and more).

My form's page has the following tag:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

My database, all my tables and all my fields are set to utf8_general_ci.

When I want to echo the database information onto the page, I use this query:

public function read_information()
{
    global $db;
    $query = "SELECT * FROM table WHERE id='1' LIMIT 1";
    return $db->select($query);
}

and return the information like so:

$info = $query->read_information();
<?php foreach ( $info as $dbinfo ) { ?>
    <pre><?php echo $dbinfo->column; ?></pre>
<?php } ?>

However, if I have French characters in my string, I need to <pre><?php echo utf8_encode($info->column); ?></pre>, and this is something I really want to avoid.

I have read up the documentation on PHP.net regarding utf8_encode/utf8_decode, htmlentities/html_entity_decode and quite a few more. However, I can't seem to figure out why I need to add a special function for every database result.

I have also tried using mysqli_query("SET NAMES 'utf8'", $mysqli); but this doesn't solve my problem. I guess what I'm looking for is some kind of shortcut where I don't have to create a function like make_this_french_friendly() type of thing.

davewoodhall
  • 998
  • 3
  • 18
  • 43
  • See my UTF-8 checklist here: http://stackoverflow.com/a/19769221/1242470 – Mate Solymosi Nov 04 '13 at 16:16
  • 1
    possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Nov 04 '13 at 16:24
  • 1
    [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/), [Handling Unicode Front To Back In A Web App](http://kunststube.net/frontback/) – deceze Nov 04 '13 at 16:25
  • You can try using `$db->set_charset("utf8");` which has saved my *"you know what"* many times. – Funk Forty Niner Nov 04 '13 at 16:29

4 Answers4

2

Ensure all the stack you are working with is set to UTF8 from db, web server, page meta etc checking things like

ini_set('default_charset', 'utf-8')

should output simple stuff then in my experience

Shaun Hare
  • 3,771
  • 2
  • 24
  • 36
0

As @deceze pointed out, this thread provided proper insight using $mysqli->set_charset('utf8');.

Community
  • 1
  • 1
davewoodhall
  • 998
  • 3
  • 18
  • 43
0

Maybe use UTF-8 without BOM encoding for your file?

header('Content-type: text/html; charset=utf-8');

... in PHP (you can also do it with "ini_set()" function) and:

<meta charset="utf-8">

... in HTML.

You have also to set the right encoding for you database tables.

Possible duplicate of "GET" method encoding French characters incorrectly in PHP

Community
  • 1
  • 1
0

Maybe your text coding is not be UTF-8.

Please look: What's different between UTF-8 and UTF-8 without BOM?

Maybe it can helps you.

Community
  • 1
  • 1