gpt4 book ai didi

php - 如何检查 mysql 表的 bool 字段以及在 php 中将 0 或 1 值转换为 true/false

转载 作者:搜寻专家 更新时间:2023-10-31 22:01:26 24 4
gpt4 key购买 nike

在我的查询中,我有 3 个 tinyint 字段,通过获取这些字段可以得到 0/1 值。但是在获取所有数据之后,我想将该结果编码为 JSON 格式,它应该得到 true/false 而不是 0/1。在我的代码中,我完成了类型转换,这给出了正确的结果。但我应该为所有我不想要的 tinyint 字段写。有什么方法可以检查 tinyint 字段并进行转换。

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);

请帮忙!!提前致谢。

最佳答案

在 PHP 中没有检查 tinyint 的真正方法

您可以做的是在您的变量前加上一个 b_ 并使用 regex/strpos/substr 对其进行过滤以将其类型转换为 bool 值。这样您只需调整查询,脚本就会为您进行转换。

$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_reviewed as b_is_reviewed,
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,'not_null');
}


function not_null($test) {
return !($test === null);
}

编辑

添加了一种将 bool 标记键恢复为原始值以正常执行代码的方法。是的,它是重复的,但可能有助于进一步实现/代码执行,而无需将所有内容重命名为 b_

关于php - 如何检查 mysql 表的 bool 字段以及在 php 中将 0 或 1 值转换为 true/false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28447847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com