PHP未定义的索引名称变量

时间:2017-07-20 07:42:45

标签: php mysql pdo

我正在测试MySQL查询,它在MySQL工作台中工作: -

SELECT patient.patient_id,
patient.patient_name_en,
patient.nationality,
visit.visit_id,
visit.date_of_visit,
visit.visit_reason,
visit.consultation_type,
visit.patient_weight,
visit.patient_height,
visit.visit_status,
visit.clinic_id,
doctor_list.doctor_name,
nurse_list.nurse_name,
consultation.complication_name,
consultation.diagnosis_id,
consultation.consultation_result,
medication.med_name,
consultation_med.given_quantity,
consultation_med.medication_collector
from visit
LEFT JOIN patient ON patient.patient_id = visit.patient_id
LEFT JOIN consultation ON visit.visit_id=consultation.visit_id 
LEFT JOIN nurse_list ON consultation.nurse_list_id = nurse_list.nurse_list_id
LEFT JOIN doctor_list ON consultation.doctor_list_id = doctor_list.doctor_list_id
LEFT JOIN consultation_med ON consultation_med.consultation_id = consultation.consultation_id
LEFT JOIN med_pharmacy ON med_pharmacy.med_pharmacy_id = consultation_med.med_pharmacy_id
LEFT JOIN medication ON medication.med_id=med_pharmacy.med_id
WHERE visit.clinic_id='361' AND visit.visit_id = '188'

enter image description here

但是当我添加到PHP文件中并向其添加绑定变量时,我收到错误说:

  

注意:未定义索引:

中的patient_id

我在这里添加了文件顶部的文件:

<?php
  include_once('../php/connection.php');
  include_once('../php/visitProfile.php');
?>

然后是visitProfile.php: -

<?php
error_reporting(E_ALL);
ini_set('display_error', 1);
require_once('../php/connection.php');

$cid = $_SESSION['clinic_id'];
$vid = $_REQUEST['pid'];

$getVisitProfile = "SELECT patient.patient_id,
patient.patient_name_en,
patient.nationality,
visit.visit_id,
visit.date_of_visit,
visit.visit_reason,
visit.consultation_type,
visit.patient_weight,
visit.patient_height,
visit.visit_status,
visit.clinic_id,
doctor_list.doctor_name,
nurse_list.nurse_name,
consultation.complication_name,
consultation.diagnosis_id,
consultation.consultation_result,
medication.med_name,
consultation_med.given_quantity,
consultation_med.medication_collector
FROM visit
LEFT JOIN patient ON patient.patient_id = visit.patient_id
LEFT JOIN consultation ON visit.visit_id=consultation.visit_id 
LEFT JOIN nurse_list ON consultation.nurse_list_id = nurse_list.nurse_list_id
LEFT JOIN doctor_list ON consultation.doctor_list_id = doctor_list.doctor_list_id
LEFT JOIN consultation_med ON consultation_med.consultation_id = consultation.consultation_id
LEFT JOIN med_pharmacy ON med_pharmacy.med_pharmacy_id = consultation_med.med_pharmacy_id
LEFT JOIN medication ON medication.med_id=med_pharmacy.med_id
WHERE visit.clinic_id=:cid AND visit.visit_id = :vid";

$execGetVisitProfile = $conn->prepare($getVisitProfile);
$execGetVisitProfile->bindValue(':cid', $cid);
$execGetVisitProfile->bindValue(':vid', $vid);
$execGetVisitProfile->execute();

$res = $execGetVisitProfile->fetchAll();
?>

当我试图回应患者身份时:

<th >Patient ID</th><td><?php echo $res['patient_id'] ?></td>

我收到了错误。

P.S。请注意,查询可能返回一个多个数组,但每个数组中的某些信息与患者ID相同。

修改

var_dump result

  

array(size = 1)0 =&gt;       数组(大小= 38)         'patient_id'=&gt;字符串'361-9001'(长度= 8)         0 =&gt;字符串'361-9001'(长度= 8)         'patient_name_en'=&gt;字符串'X Y Z'(长度= 5)         1 =&gt;字符串'X Y Z'(长度= 5)         '国籍'=&gt;字符串'叙利亚'(长度= 6)         2 =&gt;字符串'叙利亚'(长度= 6)         'visit_id'=&gt;字符串'188'(长度= 3)         3 =&gt;字符串'188'(长度= 3)         'date_of_visit'=&gt;字符串'2017-07-20'(长度= 10)         4 =&gt;字符串'2017-07-20'(长度= 10)         'visit_reason'=&gt;字符串'此访问仅用于药物收集'(长度= 49)         5 =&gt;字符串'此访问仅用于药物收集'(长度= 49)         'consultation_type'=&gt;字符串'MedicationCollection'(长度= 20)         6 =&gt;字符串'MedicationCollection'(长度= 20)         'patient_weight'=&gt;字符串'0'(长度= 1)         7 =&gt;字符串'0'(长度= 1)         'patient_height'=&gt;字符串'0'(长度= 1)         8 =&gt;字符串'0'(长度= 1)         'visit_status'=&gt;字符串'无效'(长度= 8)         9 =&gt;字符串'无效'(长度= 8)         'clinic_id'=&gt;字符串'361'(长度= 3)         10 =&gt;字符串'361'(长度= 3)         'doctor_name'=&gt;字符串'Abbas Nasser'(长度= 12)         11 =&gt;字符串'Abbas Nasser'(长度= 12)         'nurse_name'=&gt;空值         12 =&gt;空值         'complication_name'=&gt;字符串'定期药物收集访问'(长度= 35)         13 =&gt;字符串'定期药物收集访问'(长度= 35)         'diagnosis_id'=&gt;字符串'86'(长度= 2)         14 =&gt;字符串'86'(长度= 2)         'consultation_result'=&gt;字符串'药物收集'(长度= 21)         15 =&gt;字符串'药物收集'(长度= 21)         'med_name'=&gt;字符串'Isosorbide Dinitrate 10mg(Isordil 10mg)片剂'(长度= 49)         16 =&gt;字符串'Isosorbide Dinitrate 10mg(Isordil 10mg)片剂'(长度= 49)         'given_quantity'=&gt;字符串'0'(长度= 1)         17 =&gt;字符串'0'(长度= 1)         'medication_collector'=&gt;字符串'brother'(长度= 7)         18 =&gt;字符串'brother'(长度= 7)

2 个答案:

答案 0 :(得分:2)

你需要像下面这样做: -

<?php if(count($res) >1){echo $res[0]['patient_id'];}else{echo $res['patient_id']; } ?>

注意: - 此条件将检查该数组是否有多个记录。如果是,则使用0索引,否则直接获取。

答案 1 :(得分:1)

如果您想要单个记录,请在查询中写下LIMIT 1

并使用

$execGetVisitProfile->fetch(); // will return single record