SET NAMES or setEncoding utf8 in RedBeanPHP?

896 views
Skip to first unread message

HMagdy Saad

unread,
May 28, 2014, 5:46:08 AM5/28/14
to redbe...@googlegroups.com
I am using [REDBEANPHP 3.5 | easy ORM for PHP] and working with me for all parts in my app but now I have a problem while select data from database with Arabic content via

        R::setup('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
        $sql = "select * from comments";
        $comments = R::getAll($sql);
        echo "<pre>";
        die(print_r($comments));

I am getting data as 

      ["comment_content"]=> "تجربة ممتازة وتستحق التكرار"

I am searching for that and know default encoding in REDBEANPHP is UTF8 also found setEncoding function called inside RedBean_Driver_PDO class if you use the following way

      try {
            $dbh = new PDO($dsn, DB_USER, DB_PASSWORD);
            $dbh->exec('SET NAMES utf8');
            $driver = new RedBean_Driver_PDO($dbh);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
        }
        $comments = $driver->GetAll($sql);
        echo "<pre>";
        die(print_r($comments));

But Still same output 

      ["comment_content"]=> "تجربة ممتازة وتستحق التكرار"

I am sure that my db support utf8 because when I am use the following code 

    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());
    $sql = "select * from comments";
    $result = mysql_query($sql) ;
    while ($row = mysql_fetch_array($result)) {
        echo "<pre>";
        (print_r($row));
    }

I am getting correct data as 

    [comment_content] => تجربة ممتازة وتستحق التكرار

I am also trying 

    R::exec('SET NAMES utf8');

But still no hope so , How can I set that encoding to support Arabic content?

gabor

unread,
May 28, 2014, 5:45:57 PM5/28/14
to redbe...@googlegroups.com

Hi,

Have you tried to add a charset to your DSN like this?

mysql:host=localhost;dbname=DB;charset=UTF8


Note that RedBeanPHP uses PDO not the legacy MySQL driver,
so it would be interesting to see if you get the same results using
plain PDO, this might indicate a configuration problem with PDO.

more info:

http://stackoverflow.com/questions/13129634/pdo-mysql-utf-8-data-read-from-database-not-showing-correctly
http://us3.php.net/manual/en/ref.pdo-mysql.connection.php

Over here, I have have tested RedBeanPHP with MariaDB 10 (successor of MySQL) and Chinese characters and it works
perfectly, see test:

https://github.com/gabordemooij/redbean/blob/RedBeanPHP3_5_X/testing/RedUNIT/Base/Utf8.php


Btw, I dont have a clue what I am saying there in Chinese.
Do you have some funny arabic text for me to add to the unit test?

cheers
Gabor
Reply all
Reply to author
Forward
0 new messages