Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've this strange problem showing up lately. I'm currently developing on a Windows environment while deploying on a Linux server, I know that isn't ideal but I can't do much about it at this stage.

All I'm doing is getting the data from database and then returning a JSON response of the resulting array, but the result is different causing problem in my front-end app.

I'm getting this on windows:

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

and this on Linux:

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

I'm actually using Laravel framework and so the syntax is simply this:

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

Which behind the scene is doing this:

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

So unluckily I don't have a hook where to set JSON_NUMERIC_CHECK option.

Before refactoring my code, is there a way to force JSON_NUMERIC_CHECK on all json_encode calls? I'm using the same database to fetch the data so I guess it could be a platform-related problem?

EDIT:

Further investigations made me think that the guilty may be the database drivers. If I dump the data on windows I'm getting this:

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

while on Linux it's:

array
     'id' => string '40' (length=2)
     'name' => string 'test' (length=4)
share|improve this question
    
what version php is? I'll take a look into php-src for details. –  gureedo Jul 31 '13 at 5:05
    
Well I have 5.3.9 on Windows and 5.4.17 on Linux but I don't think it's a PHP problem, but more a database's driver problem. I've added the dump of the data returned from database to my question. –  Ingro Jul 31 '13 at 7:33
    
Ok, then maybe you say what DB you use and what drivers you use? –  gureedo Jul 31 '13 at 7:58
    
I'm using a Mysql database and querying it via PDO. Well I'm not querying it directly, I'm using the built-in ORM library from Laravel Framework, Eloquent. –  Ingro Jul 31 '13 at 8:07
1  
PDO uses MySQL API driver. Only mysqlnd provides correct data types. So querying integer return integer. Another drivers return all data as string. Also you can check PDO does not use statement prepare emulation. –  gureedo Jul 31 '13 at 11:04

1 Answer 1

up vote 3 down vote accepted

Laravel uses PDO and PDO uses MySQL driver. It's mysql or mysqlnd.

Only mysqlnd provides correct data types. So querying integer return integer. Another drivers return all data as string. Also you can check that PDO does not use statement prepare emulation.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.