Json_encode returns integer values ​​in windows and string in linux

I have got this strange problem lately. I am currently developing a Windows environment when deploying to a Linux server, I know this is not ideal, but at this point I can not do much.

All I do is get the data from the database and then return the JSON response of the resulting array, but the result is different from the problem in my front-end application.

I get this in windows:

{ "id":40, "name":"test" } 

and this is on Linux:

 { "id":"40", "name":"test" } 

I actually use the Laravel framework, so the syntax is just like this:

 $user = User::find($id); return Response::json($user->toArray()); 

What the scene does:

 $this->data = json_encode($data); 

Unfortunately, I do not have a hook where you can set the JSON_NUMERIC_CHECK parameter.

Before refactoring my code, is there a way to force JSON_NUMERIC_CHECK on all json_encode calls? I use the same database to retrieve data, so I assume this could be a platform issue?

EDIT:

Further research made me think that database drivers might be guilty. If I reset the data in the windows, I get the following:

  array 'id' => int 40 'name' => string 'test' (length=4) 

and on Linux it is:

 array 'id' => string '40' (length=2) 'name' => string 'test' (length=4) 
+4
source share
1 answer

Laravel uses PDO, and PDO uses MySQL driver. This is mysql or mysqlnd.

Only mysqlnd provides the correct data types. Thus, an integer query returns an integer. Other drivers return all data as a string. You can also verify that PDO does not use instruction preparation emulation.

+5
source

Source: https://habr.com/ru/post/1494247/


All Articles