0

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.

hman_codes
  • 794
  • 9
  • 24
  • 1
    You need to set the same charset throughout the application. See [**this answer**](https://stackoverflow.com/a/31899827/4535200) or [UTF8 All The Way Through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through). – Qirel Feb 11 '19 at 12:37
  • @Qirel can you check my edited answer and tell me why am I able to retrieve the inserted data correctly in that case? – hman_codes Feb 11 '19 at 13:19
  • Hard to say, you haven't provided enough code for us to tell. Go through the checklist given in the first link (a previous answer of mine), or read the second link for more in-depth information. – Qirel Feb 11 '19 at 13:36
  • @Qirel I found it. I thought I made all there is in the comment you provided. Turns out I missed the PDO section. Thanks a lot. – hman_codes Feb 11 '19 at 16:26

1 Answers1

0

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.

hman_codes
  • 794
  • 9
  • 24