2019-02-11 12:32
浏览 45


As the title says, question marks are returned only when I run mysqld.exe and httpd.exe from within my java program. However, when I run xampp.exe and start apache and mysql from the UI it works perfectly. I am using slim 3 framework with mysql database.

I've tried setting character set in every possible way: setting the slim application configurations, setting the database character set, writing response using JSON_UNESCAPED_UNICODE, etc.

// Slim application configuration
$config = [ 'charset' => 'utf-8' .....]

// .htacess file
AddDefaultCharset utf-8

// my.ini was already configured

// $response in php function
return $response->withHeader('Content-Type', 'application/json')->withStatus(200)->withJson($json_response, null, JSON_UNESCAPED_UNICODE);

However, since the problem is being resolved when I manually run xampp I'm thinking maybe there is some file that should run alongside the ones mentioned above. If not, is there a way to work around this?

EDIT: I tried inserting data through the api instead of manually inserting it using a query in phpmyadmin. This inserted data was the only line that returned correctly. Note that I inserted the data while the server was run from within my java program.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhan8395 2019-02-11 16:31

    I fixed the problem by setting PDO correctly. (Check OP comments)

    $pdo = new PDO ("mysql:host=localhost;dbname=database;charset=utf8", "user", "pass");

    The reason the data inserted through the api was being retrieved correctly is that it was sent in a the same charset that PDO was set to. When I tried to retrieve it after setting the PDO it was malformed.

    点赞 打赏 评论

相关推荐 更多相似问题