underlying PDO not processing latin1 charset in dsn argument

270 views
Skip to first unread message

Bill Beck

unread,
May 30, 2014, 1:24:20 PM5/30/14
to redbe...@googlegroups.com
Hi all,

I love redbean, but recently found out a legacy DB in mysql is using latin1 encoding. So I tried to change my R::setup like this:

\R::setup('mysql:host=localhost;dbname=nu_view;charset=latin1', ..

But my query gets funny characters:  

Augustin Dupré



The same dsn in PDO on the same database gets the correct results:

$dbc = new PDO('mysql:host=localhost;dbname=nu_view;charset=latin1', ...

same query returns:
Augustin Dupré



Anythoughts? Is the redbean syntax for setting up the PDO connection different somehow, or does not support non-utf8 charsets?

Thanks a ton.


-Bill B

gabor

unread,
May 30, 2014, 5:30:02 PM5/30/14
to redbe...@googlegroups.com

Hi,

Simply pass your own PDO instance to R::setup(), it will then use your PDO object for all queries.

cheers,
Gabor

Bill Beck

unread,
May 30, 2014, 9:06:20 PM5/30/14
to redbe...@googlegroups.com
HI Thanks for the fast reply!

I tried this and no dice:

$db = new \PDO('mysql:host=localhost;dbname=nu_view;charset=latin1', $user, $pass);

\R::setup($db);

. Any other thoughts? No error triggered, just it's returning bad encodings, whereas direct querying via my own pdo instance returns correct encoding. I've verified via mysql command line that the server and tables are latin1.

Any change that the R facade is using the passed PDO instance but altering attributes etc?

Could be something to do with the static calls? How I'm using it is this:

class nuview {

   __construct(){
      $db = new \PDO( etc)  
      R::setup($db);
  }
}



class item extends nuview {

 __construct() {
   parent::__construct();
}

function get_item($id){
  $rows = \R::getAll( 'SELECT * FROM items');
  return $rows;
}
}



thanks!!

-bb

gabor

unread,
May 31, 2014, 4:40:36 AM5/31/14
to redbe...@googlegroups.com


Hi,

Logical, I glanced at the code and it only extracts the DSN and then creates its own PDO instance. I forgot.

Have you tried:

R::exec('SET NAMES [yourcharset] ');
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html


If that does not work, open the source code and comment out:

$this->setEncoding();

in the __construct function of RPDO.

In the new version of RedBeanPHP (4.1) you will be able to set a PDO instance.
Sorry for the inconvenience. RedBeanPHP has been designed to solely use UTF8,
because this kills an entire class of bugs and security issues (relating to XSS).

However if you have a legacy database I can understand this is a bit of a problem.
However, I believe that UTF8 is really a better charset and maybe the best solution to
the problem is to convert your database:

http://makezine.com/2007/05/08/mysql-database-migration-latin/

Hope this helps,

cheers,
Gabor

Bill Beck

unread,
Jun 2, 2014, 11:38:37 AM6/2/14
to redbe...@googlegroups.com
I couldn't agree more about moving to utf8, and am working to convert. It's been tricky trying to get that to work, because it's not clear if the data is actually latin1, or just the system/db/client connection. But I think I'm close. For anyone else reading, here are two important reads to help prevent double encodings, or wrong conversions:

http://docforge.com/wiki/Mysqldump

I'll use your work around for now, I appreciate that.

Redbean is very cool, and fun to use, so thanks for it!

-bb

gabor

unread,
Jun 2, 2014, 2:38:01 PM6/2/14
to redbe...@googlegroups.com


You're welcome!
Good luck!

cheers
Gabor
Reply all
Reply to author
Forward
0 new messages