以下查询包含加密的患者姓名字段:
SELECT
patient.patient_id as patient_id2,
CONVERT(aes_decrypt(patient_name_en, :encKey) using utf8mb4) as patient_name_en2,
visit.visit_id as visit_id,
visit.date_of_visit as date_of_visit,
medication.med_name as med_name,
consultation_med.medication_collector as medication_collector,
consultation_med.given_quantity as given_quantity,
(SELECT
sum(consultation_med.given_quantity)
FROM
med_pharmacy
LEFT JOIN
medication
ON
med_pharmacy.med_id=medication.med_id
LEFT JOIN
consultation_med
ON
consultation_med.med_pharmacy_id=med_pharmacy.med_pharmacy_id
LEFT JOIN
consultation
ON
consultation.consultation_id=consultation_med.consultation_id
LEFT JOIN
visit
ON
visit.visit_id = consultation.visit_id
LEFT JOIN
patient
ON
patient.patient_id = visit.patient_id
WHERE med_pharmacy.med_id=:mid) as sumGiven
FROM
med_pharmacy
LEFT JOIN
medication
ON
med_pharmacy.med_id=medication.med_id
LEFT JOIN
consultation_med
ON
consultation_med.med_pharmacy_id=med_pharmacy.med_pharmacy_id
LEFT JOIN
consultation
ON
consultation.consultation_id=consultation_med.consultation_id
LEFT JOIN
visit
ON
visit.visit_id = consultation.visit_id
LEFT JOIN
patient
ON
patient.patient_id = visit.patient_id
WHERE
med_pharmacy.med_id=:mid
and
med_pharmacy.clinic_id=:cid
and
visit.visit_status = :visitStatus
ORDER BY
visit.date_of_visit DESC
在MySQL中运行此查询时,我可以看到患者的姓名。但是当我将数据作为JSON数组从PHP脚本发送到Ajax时,患者名称在网络选项卡返回的数组和屏幕上显示为null。
这是PHP文件:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('connection.php');
header('Content-type: application/json');
$cid = $_SESSION['clinic_id'];
$mid = $_POST['mid'];
$encKey = ...//
$res_distribution = array();
$getMedicationsDist = "SELECT
patient.patient_id as patient_id2,
CONVERT(aes_decrypt(patient_name_en, :encKey) using utf8mb4) as patient_name_en2,
visit.visit_id as visit_id,
visit.date_of_visit as date_of_visit,
medication.med_name as med_name,
consultation_med.medication_collector as medication_collector,
consultation_med.given_quantity as given_quantity,
(SELECT
sum(consultation_med.given_quantity)
FROM
med_pharmacy
LEFT JOIN
medication
ON
med_pharmacy.med_id=medication.med_id
LEFT JOIN
consultation_med
ON
consultation_med.med_pharmacy_id=med_pharmacy.med_pharmacy_id
LEFT JOIN
consultation
ON
consultation.consultation_id=consultation_med.consultation_id
LEFT JOIN
visit
ON
visit.visit_id = consultation.visit_id
LEFT JOIN
patient
ON
patient.patient_id = visit.patient_id
WHERE med_pharmacy.med_id=:mid) as sumGiven
FROM
med_pharmacy
LEFT JOIN
medication
ON
med_pharmacy.med_id=medication.med_id
LEFT JOIN
consultation_med
ON
consultation_med.med_pharmacy_id=med_pharmacy.med_pharmacy_id
LEFT JOIN
consultation
ON
consultation.consultation_id=consultation_med.consultation_id
LEFT JOIN
visit
ON
visit.visit_id = consultation.visit_id
LEFT JOIN
patient
ON
patient.patient_id = visit.patient_id
WHERE
med_pharmacy.med_id=:mid
and
med_pharmacy.clinic_id=:cid
and
visit.visit_status = :visitStatus
ORDER BY
visit.date_of_visit DESC";
$execGetMedicationsDist = $conn->prepare($getMedicationsDist);
$execGetMedicationsDist->bindValue(":visitStatus", "Active");
$execGetMedicationsDist->bindValue(":encKey", $mid);
$execGetMedicationsDist->bindValue(":mid", $mid);
$execGetMedicationsDist->bindValue(":cid", $cid);
$execGetMedicationsDist->execute();
//$getExecGetMedications = $execGetMedications->fetchAll();
$i=0;
foreach($execGetMedicationsDist as $var2)
{
$res_distribution[$i]=$var2;
$i++;
}
echo json_encode($res_distribution);
?>
和AJAX电话:
$.ajax({
url: '../php/getMedicationDistributionReport.php',
type: 'POST',
data: {mid: searchTxt},
dataType: 'JSON',
success:function(resp2)
{
//append data
$("#med_table_distribution tr").fadeOut(400);
$("#med_table_distribution #after_tr_distribution").before("<tr class='bg-info'><th colspan='10' style='text-align: center'>Patient who received this medication =</th></tr><tr><th>Patient ID</th><th colspan='3'>Patient Name</th><th>Visit ID</th><th>Date of Visit</th><th colspan='2'>Medication Name</th><th>Collector</th><th>Quantity Given</th></tr>");
$.each( resp2, function(key, result2)
{
//var pid = result['patient_id'];
var sumGiven = parseInt(result2['sumGiven']);
//var profileBtn = "<a id='profileBtn'><span class='badge badge badge-info' style='background-color: #0090ff'>Patient Profile</span></a>"
$("#after_tr_distribution").after("<tr id="+result2['patient_id']+"><td>"+result2['patient_id2']+"</td><td colspan='3'>"+result2['patient_name_en2']+"</td><td>"+result2['visit_id']+"</td><td>"+result2['date_of_visit']+"</td><td colspan='2'>"
+result2['med_name']+"</td><td>"+result2['medication_collector']+"</td><td>"
+result2['given_quantity']+"</td></tr>");
});
//console.log(sumGiven);
},
error:function(resp2)
{
console.log(resp2);
}
})
答案 0 :(得分:0)
您的错误位于
foreach($execGetMedicationsDist as $var2)
{
$res_distribution[$i]=$var2;
$i++;
}
应该是
while($var2 = $execGetMedicationsDist->fetch(PDO::FETCH_ASSOC))
{
$res_distribution[]=$var2;
}
因为$execGetMedicationsDist
是PDOStatement object我已经删除$i++
,因为您不再使用它而不是必需的。有关PDO::fetch