Keyword for Template Document coming from extra table

I am trying to create new keywords for the Template Documents, and I am getting trouble while inserting my Mysql code in download_template.php…

The code is: SELECT lname, fname, room, bed FROM patient_data, inpatient, bed_occupation, stay WHERE bed_occupation.bedid = inpatient.bedid and patient_data.pid = stay.PatientId AND bed_occupation.PatientId = patient_data.pid

Does anyone have an answer?

Thanks!