0

I wrote a web application and uploaded it to remote server. I used PHP5, MySQL and AngularJS.

differences from local and remote installation are:

----------------------------------------------
|        | local host   | remote host        |
----------------------------------------------
| OS     | Fedora 22    | Ubuntu 14.04.2 LTS |
----------------------------------------------
| PHP    | 5.6.10       | 5.5.9              |
----------------------------------------------
| Apache | 2.4.12       | 2.4.7              |
----------------------------------------------
| MySQL  | 5.6.25       | 5.5.43             |
----------------------------------------------

maybe my problem is connected to version differences or maybe not but in both cases I would like to understand what is causing this difference.

problem description: when requesting some resource which will return a json encoded list of objects (resulted from a query to MySQL server) from local server it will be parsed and will look like this

[{id : 1, name: "some name 1"}, {id: 2, "some name 2"}]

but when request is made to remote server, same data will be parsed as:

[{id : "1", name: "some name 1"}, {id: "2", "some name 2"}]

please note that numbers are interpreted as strings and in both cases Content-Type is equal to application/json.

if some other info is needed, I would be glad to provide it.

Scantlight
  • 253
  • 3
  • 13

1 Answers1

1

If you're using PDO prepared statements, it it possible that php have been compiled with different mysql libraries: mysqlng on localhost and libmysqlclient on remote. Check out PHP documentation page about choosing MySQL library to know the difference.

When PHP encodes JSON, it checks only the variable type, not variable content. Thus this code:

$array = array('id' => '1'); // notice '1' has string type
echo json_encode($array);
// {"id": "1"}

produces different output than:

$array = array('id' => 1); // notice '1' has int type
echo json_encode($array);
// {"id": 1}

The point is that libmysqlclient always returns column values as string type, while mysqlng may use PHP's int type for MySQL INT columns. That's why you have array of objects with ID of type int locally and array of objects with ID of type string on remote.

To check out what library does your PHP instance use, see this stackoverflow question.

Community
  • 1
  • 1
ptkoz
  • 2,388
  • 1
  • 20
  • 28