Hello everybody,
I am developing an extremely simple website using CodeIgniter. First of all I’d like to say that I did all the things listed in Phil Sturgeon’s blog post.
Now there are two things that are not working properly:
1) First, UTF-8 is not detected by CodeIgniter. This means that despite all the steps described in Phil Sturgeon’s blog post, data retrieved from the database are not treated as UTF-8.
2) The database system in codeigniter fails to retrieve data in UTF-8.
I will explain how I came to those two conclusions:
Here is a method that I use in one of my models:
public function get_one($id)
{
$this->db->select('idTexte, titre_fr AS titre, texte_fr AS texte, intro_fr AS intro')
->from($this->_table)
->where($this->_primary_key, $id)
->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1)
return $query->row();
else
return FALSE;
} // End of get_one
And here is the result I get from the database (result displayed with var_dump()):
object(stdClass)#19 (4) {
["idTexte"]=>
string(7) "aPropos"
["titre"]=>
string(42) "À Propos - àpropos Présentation"
["texte"]=>
string(156) "<p> c'est cool, c'est super!!!!!! HéHé!</p>
<p> Ãƒâ‚¬ Propos - àpropos</p>
<p>À Propos - àpropos</p>"
["intro"]=>
string(13) "<p> </p>"
}
Now look at the equivalent method using a new mysqli connection:
public function get_one($id)
{
$mysqli = new mysqli('localhost', 'root', 'root', 'the_database');
$_q = 'SELECT titre_fr AS titre, texte_fr AS texte, intro_fr AS intro FROM table_textes';
if ($result = $mysqli->query($_q))
{
while ($row = $result->fetch_object())
{
var_dump($row); // DEBUG <-
die();
}
}
} // End of get_one
And here is the var_dump() output:
object(stdClass)#19 (3) {
["titre"]=>
string(35) "À Propos - à propos Présentation"
["texte"]=>
string(142) "<p> c'est beau, c'est cool, c'est super!!!!!! HéHé!</p>
<p> Ã€ Propos - à propos</p>
<p>À Propos - à propos</p>"
["intro"]=>
string(13) "<p> </p>"
}
There is clearly something wrong with the CodeIgniter database system. The difference between the two var_dump outputs is that if I use PHP’s utf8_decode on the retrieved data, the mysqli output is displayed correctly:
CodeIgniter’s output:
object(stdClass)#19 (4) {
["idTexte"]=>
string(7) "aPropos"
["titre"]=>
string(35) "� Propos - à propos Présentation"
["texte"]=>
string(142) "<p> c'est beau, c'est cool, c'est super!!!!!! HéHé!</p>
<p> Ã? Propos - Ã propos</p>
<p>Ã? Propos - Ã propos</p>"
["intro"]=>
string(13) "<p> </p>"
}
MySQLi’s output:
object(stdClass)#19 (3) {
["titre"]=>
string(32) "À Propos - à propos Présentation"
["texte"]=>
string(136) "<p> c'est beau, c'est cool, c'est super!!!!!! HéHé!</p>
<p> À Propos - à propos</p>
<p>À Propos - à propos</p>"
["intro"]=>
string(13) "<p> </p>"
}
These two last code snippets prove my first point: CodeIgniter doesn’t recognize UTF-8:the use of utf8_decode() should not be necessary for MySQLi’s output to get displayed correctly.
Now, I prepended the title of this post with BUG because it’s the most likely explenation. Also, I downloaded a fresh version of CodeIgniter and reran all the tests and I got the same results, which proves once again that those two problems I have are actually bugs.
Can anyone confirm what I am saying, and help me solve the problem please?
