PHP mysql_fetch_object,所有属性都是字符串吗? [英] PHP mysql_fetch_object, all properties are string?
问题描述
任何人都可以帮忙吗?
mysql_fetch_object以字符串类型返回所有属性. 我需要将对象转换为JSON,但保留数字和布尔值.
mysql_fetch_object returns all properties as type string. I need to convert the object to JSON but preserving the numerical and Boolean.
对于所有查询而言,解析结果JSON的速度都很慢. 这是我的查询var_dump的结果.
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)
...
"Habilitado"属性在数据库中为BOOLEAN(我已经尝试过使用BIT数据类型,但结果相同).
The 'Habilitado' property is BOOLEAN in the DataBase (I already tried with BIT data type, but the same result).
然后使用json_encode进行JSON:
Then JSON with json_encode:
{"DTOList":
{"idUsuario":"1",
"Email":"user@theemail.com.ar",
"Password":"1234","Nombre":"Sebasti\u00e1n","Apellido":"Black","Habilitado":"1"...
推荐答案
那是正确的. MySQL以字符串形式返回所有内容,除了NULL
照原样传递.
That is correct. MySQL returns everything as strings, except NULL
which is passed as is.
要注意的另一件事是BOOLEAN
只是TINYINT(1)
的别名,其中0
是FALSE
,所有其他值都是TRUE
.
Another thing to note is that BOOLEAN
is just an alias for TINYINT(1)
where 0
is FALSE
and all other values are TRUE
.
这篇关于PHP mysql_fetch_object,所有属性都是字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!