PHP mysql_fetch_object, all properties are strings?

Can anyone help?

mysql_fetch_object returns all properties as a type string. I need to convert the object to JSON, but keep the numeric and logical.

Parsing the resulting JSON is very slow for all requests. This is the result of my var_dump query.

$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 a DataBase (I already tried with the 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"... 
0
source share
2 answers

It is right. MySQL returns everything as strings, except for NULL , which is passed as is.

One more note: BOOLEAN is just an alias for TINYINT(1) , where 0 is FALSE and all other values ​​are TRUE .

+2
source

I had a similar problem, although I used FETCH_ASSOC instead of FETCH_OBJ in PDO. You can “draw” (tooltip) the fields and they will appear as you expect in JSON. In your example, this might work (put it before json_encode ):

 $obj->idUsuario = (int) $obj->idUsuario; $obj->Habilitado = (bool) $obj->Habilitado; 
+1
source

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


All Articles