[英]How to check boolean field of mysql table and convertion of 0 or 1 value to true/false in php
在我的查詢中,我有3個tinyint字段,通過提取給出0/1值。 但是在獲取所有數據之后我想將該結果編碼為JSON格式,該格式應該為真/假而不是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
和integer
。 因此,您需要列出要轉換的字段。 縮短版本如下所示:
$a = ['a' => 1, 'b' => 100, 'c' => 0 ];
$bools = ['a', 'c']; // list of fields to transform
$result = array_map(function($item) use($bools,&$a) {
$key=key($a); next($a); // we need a key to check it’s name
return in_array($key, $bools) ? (bool)$item : $item;
}, $a);
print_r(json_encode($result, true));
//⇒ {"a":true,"b":100,"c":false}
后者將integer
為bool
如果且只有key
在$bools
數組中列出。 有關詳細信息,請參閱array_map
文檔 。
希望能幫助到你。
在PHP中沒有真正的方法來檢查tinyint
您可以做的是使用b_前置變量並使用regex / strpos / substr來過濾它以將其轉換為布爾值。 這樣您只需調整查詢,腳本就會為您進行投射。
$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);
}
編輯
添加了一種方法,將布爾標記的鍵恢復為正常代碼執行的原始值。 是的,它是重復的,但可能有助於進一步實現/代碼執行,而無需將所有內容重命名為b_
嘗試編寫另一個函數:
private function isTrue($input){
if ((bool)$input){
return "true";
}else{
return "false";
}
}
當使用0或1調用時,返回文本“true”或“false”。 然后為每個要編碼的tinyint添加調用:
...
$row['hospitalized'] = isTrue($row['hospitalized']);
...
JS代碼:
if(1)
alert('true');
if(0)
alert('false');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.