在我的查询中,我有3个tinyint字段,它们通过获取给出0/1值。但是在获取所有我想要将结果编码为JSON格式的数据之后,应该得到true/false而不是0/1。 在我的代码中,我做了类型转换,它给出了正确的结果。但我应该写下所有我不想要的tinyint字段。 是否有任何方法来检查tinyint字段和转换。如何检查mysql表的boolean字段,并将0或1的值转换为true/false在php
PHP CODE:
$result= mysql_query("select incident_main.inc_id as incident_id,
ward_master.ward_id as 'ward_id',
GROUP_CONCAT(incident_log.inclog_date) as revisit_dates ,
inc_GISlat as lat,
inc_GISlon as lon,
inc_closed as b_is_closed,
inc_closeDate as closed_date,
incident_statuscd.inc_status_desc as status,
inc_date as 'incident_date',
inc_patientName as 'patient_name',
inc_patientAge as 'patient_age',
inc_patientGender as 'patient_gender',
inc_patientMobile as'patient_mobile' ,
inc_patientAddress as 'patient_address',
inc_type as type,
inc_typeOther as 'type_other',
inc_diagnosis as diagnosis,
inc_recurrence as recurrence,
inc_hospitalized as b_hospitalized,
inc_treatment as treatment ,
inc_treatmentOther as 'treatment_other'
from
incident_main,
incident_statuscd ,
ward_master,
incident_log
where
inc_status=incident_statuscd.inc_status_id
and
incident_main.inc_patientWard=ward_master.ward_id
and incident_log.inc_id=incident_main.inc_id ")
or die(mysql_error);
while($res = mysql_fetch_assoc($result)) {
foreach($res as $key => $value) {
if(substr($key, 0, 2) === "b_") {
// assign new value
$res[substr($key,2,strlen($key))] = !!$value;
//clean up old value
unset($res[$key]);
};
}
$data=array_filter($res);
echo json_encode($data, true);
请帮忙!! 在此先感谢。
JS也有类型转换。所以不要这样做,如果你不想。 – Vick 2015-02-11 06:36:37
可以请你更具体地解释....在哪里以及如何? – user092 2015-02-11 06:40:11
[请参阅此链接](http://stackoverflow.com/questions/16313222/how-to-convert-1-to-true-or-0-to-false-upon-model-fetch) – 2015-02-11 06:48:52