0

Can anyone help?

mysql_fetch_object returns all properties as type string. I need to convert the object to JSON but preserving the numerical and Boolean.

Parse the resulting JSON is very slow for all queries. This is the result of my query var_dump.

$obj = mysql_fetch_object($result)
var_dump($obj);
...
object(stdClass)[10]
public 'idUsuario' => string '1' (length=1)
public 'Email' => string 'user@theemail.com.ar' (length=23)
public 'Password' => string '1234' (length=4)
public 'Nombre' => string 'Sebastián' (length=10)
public 'Apellido' => string 'Black' (length=7)
public 'Habilitado' => string '1' (length=1)
...

The 'Habilitado' property is BOOLEAN in the DataBase (I already tried with BIT data type, but the same result).

Then JSON with json_encode:

{"DTOList":
{"idUsuario":"1",
"Email":"user@theemail.com.ar",
"Password":"1234","Nombre":"Sebasti\u00e1n","Apellido":"Black","Habilitado":"1"...
Cheva
  • 331
  • 5
  • 12
  • 1
    There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. – Schleis May 17 '13 at 17:46
  • 1
    It would be wise to begin using mysqli or pdo as the use of mysql_ functions has been deprecated – Kevin May 17 '13 at 17:46
  • 2
    You might want to check these questions out; they won't help for the mysql functions, but they probably represent the way of doing it with "modern" methods: http://stackoverflow.com/questions/2430640/get-mysql-query-results-as-their-native-data-type http://stackoverflow.com/questions/1197005/how-to-get-numeric-types-from-mysql-using-pdo – Matt Gibson May 17 '13 at 17:50

2 Answers2

2

That is correct. MySQL returns everything as strings, except NULL which is passed as is.

Another thing to note is that BOOLEAN is just an alias for TINYINT(1) where 0 is FALSE and all other values are TRUE.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

I had a similar problem, although I used FETCH_ASSOC instead of FETCH_OBJ in PDO. You can "cast" (hint) the fields and they will show up as you expect in the JSON. In your example, this might work (place it before the json_encode):

$obj->idUsuario = (int) $obj->idUsuario;
$obj->Habilitado = (bool) $obj->Habilitado;
Andrew67
  • 357
  • 2
  • 7