我使用mysql和php从我的数据库中获取了json数据,现在想要使用$ .ajax函数将其传回来并动态创建样条图表,任何帮助或示例都将受到赞赏。对于重复日期的多次血液检查,我怎样才能显示一个具有多个血液检测结果的日期。
这是我的PHP代码
header('Set Access-Control-Allow-Origin: *');
header('Content-type: application/json');
require_once"conn.php";
if($_SERVER['REQUEST_METHOD']=='GET'){
//recieve credentials from the user
$nhsno = mysqli_real_escape_string($conn, $_GET['pnhsno']);
//check the variables recieved are not empty
if($nhsno != ''){
$sql = "SELECT date,name,tname,value FROM examination e, testresults t, examtype ex, testname tn
WHERE e.patientnhs_no = '$nhsno' and e.etype_id = '1' and
e.examination_id = t.examination_id and e.etype_id = ex.etype_id and t.tname_id = tn.tname_id ";//create an sql statement
$result = $conn->query($sql);//run sqlm statement
$row = $result->fetch_assoc(); //fetch row of data
foreach ($result as $row){
$return[]=array('date'=>$row['date'],
'name'=>$row['name'],
'test name'=>$row['tname'],
'value'=>$row['value']);
}
}
}
$conn = null;
echo json_encode($return);
?>
这是我的json输出
[{"date":"2004-07-05","name":"blood test","test name":"t3","value":"6.8"}, {"date":"2004-07-05","name":"blood test","test name":"t4","value":"29"},{"date":"2004-07-05","name":"blood test","test name":"tsh","value":"0.01"},{"date":"2004-07-05","name":"blood test","test name":"thyroglobulin level","value":"0.5"},{"date":"2005-06-15","name":"blood test","test name":"t3","value":"5.2"},{"date":"2005-06-15","name":"blood test","test name":"t4","value":"30"},{"date":"2005-06-15","name":"blood test","test name":"tsh","value":"0.02"},{"date":"2005-06-15","name":"blood test","test name":"thyroglobulin level","value":"0.5"}]
答案 0 :(得分:0)
因为您混合了很多东西,并且在循环之前获取了1行。
它应该是这样的:
$result = $conn->query($sql); //run sqlm statement
//Do not need this!
//$row = $result->fetch_assoc(); //fetch row of data
//And $row = $result... not opposite.
while ($row = $result->fetch_assoc()) {
$return[] = array('date' => $row['date'],
'name' => $row['name'],
'test name' => $row['tname'],
'value' => $row['value']);
}