php - Lumen MySQL query not handling UTF8 value as expected -
i'm working against database using utf8 encoding , has many user names contain special characters, such "Ғђ ▫ sony"
when querying user table, lumen responds incorrect data. i've tried querying same table using mysqli
, pdo
, receive expected results. set sample route test it:
$app->get("charset", function() { $mysqli = new mysqli("localhost", "user", "password", "database"); $res = $mysqli->query("select name users id = 1"); $dbh = new pdo('mysql:host=localhost;dbname=database', "user", "password"); $stmt = $dbh->query("select name users id = 1"); $lumen = db::select("select name users id = 1"); return response()->json([ "mysqli" => $res->fetch_assoc(), "pdo" => $stmt->fetchall(pdo::fetch_assoc), "framework" => $lumen ]); });
when accessing route, following response:
{ "mysqli": { "name": "Ғђ ▫ sony" }, "pdo": [ { "name": "Ғђ ▫ sony" } ], "framework": [ { "name": "Ò’Ñ’ â–« sony" } ] }
here's screenshot of response in case text above not display correctly:
as far can tell, lumen's mysql config defaults utf8 , unchangeable - found following in vendor/laravel/lumen-framework/config/database
:
'mysql' => [ 'driver' => 'mysql', 'host' => env('db_host', 'localhost'), 'database' => env('db_database', 'forge'), 'username' => env('db_username', 'forge'), 'password' => env('db_password', ''), 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => env('db_prefix', ''), 'timezone' => env('db_timezone','+00:00'), 'strict' => false, ],
i'm @ loss causing this. else can try track down discrepancy?
this answer based on previous comments above.
the mysql connection charset defines encoding used communication between mysql client (php) , server. not matter encoding used internal encoding in actual mysql tables. mysql server automatically convert data between table encoding , connection encoding. connection encoding defines format in expect data mysql , in format inserting data mysql.
are sure data correctly encoded in utf8 in database?
seems using utf8 lumen db connection (if default), not use utf8 mysqli or pdo connection examples. same result if set mysqli charset using $mysqli->set_charset("utf8");
, pdo charset using new pdo('mysql:host=localhost;dbname=database;charset=utf8', "user", "password");
?
based on code , output example seem correctly getting data in utf8 lumen db connection output not displayed utf8.
this explains why mysqli , pdo output shown correctly because not returning data in utf8 (because have not set connection charset utf8) default seem match whatever encoding you're displaying output in (apparently "latin1" or compatible).
if viewing output in web browser make sure output page charset defined correctly (e.g. using header).
edit:
btw should not matter connection encoding used in other system inserts data long connection encoding matches encoding of data sent through connection.
setting connection encoding latin1 means string data latin1 when select. seem output handled latin1 instead of utf-8. better if fixed script output correctly display "as utf-8" instead if output environment (e.g. web browser) supports it. because otherwise have problems if need handle characters can not shown in latin1. though if output cli terminal/console instead of course should use same encoding default terminal encoding (which can utf-8 or else). prefer have linux terminals configured utf-8.
Comments
Post a Comment