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 '[email protected]' (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":"[email protected]",
"Password":"1234","Nombre":"Sebasti\u00e1n","Apellido":"Black","Habilitado":"1"...
3
  • 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. Commented May 17, 2013 at 17:46
  • 1
    It would be wise to begin using mysqli or pdo as the use of mysql_ functions has been deprecated Commented May 17, 2013 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: stackoverflow.com/questions/2430640/… stackoverflow.com/questions/1197005/… Commented May 17, 2013 at 17:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.