<?php
$link = mysql_connect('localhost', 'root', 'admin')
or die('There was a problem connecting to the database.');
mysql_select_db('hospitalmaster');
$hnum = (int)$_POST["hnum"];
$sql = "SELECT d.doctorid, d.doctorname
from hospitalmaster.doctor_master d
inner join pharmacymaster.pharbill e
where e.hnum = '$hnum'
and e.presid = d.d_empno
group by e.presid";
$result = mysql_query($sql);
$response = array();
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$response = $row;
}
echo json_encode(array("Doctor"=>$response));
} else {
echo ("no DATA");
}
?>
我有上面显示的api,但是这个api正在返回我作为json对象而不是json数组?我想知道如何将dotorid作为一个阵列的医生名称,因为我有很多医生的名字和身份证,我希望每个医生和他们相应的身份证作为一个单独的数组,现在他们作为单独的对象返回。由于这是我第一次编写api,我不知道如何修改代码。
答案 0 :(得分:3)
每次循环都是在编写数组。
mysqli_
您应该使用
的建议mysqli_
或PDO。以下是对<?php $link = mysqli_connect('localhost', 'root', 'admin','hospitalmaster') or die('There was a problem connecting to the database.'); $sql = "SELECT d.doctorid, d.doctorname from hospitalmaster.doctor_master d inner join pharmacymaster.pharbill e where e.hnum = ? and e.presid = d.d_empno group by e.presid"; $stmt = $link->prepare($sql); $stmt->bind_param('i', (int)$_POST["hnum"]); $stmt->execute(); if ($stmt->num_rows() > 0) { $result = $stmt->get_result(); $response = array(); while ($row = $result->fetch_assoc()) { $response[] = $row; } echo json_encode(array("Doctor"=>$response)); } else { echo ("no DATA"); } ?>
pec