我在使用$.getJSON
請求獲取JSON數據時遇到問題。在一個PHP文件中,我有我的功能。該代碼是:無法讓函數調用在PHP中工作
function get_topchart($dbc, $var_ziekenhuis_id, $var_department_id) {
if($var_department_id == "Not Entered") {
$query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' ORDER BY afdeling DESC";
$result = mysqli_query($dbc,$query);
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
//return(json_encode($data));
} else {
$query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' and afdeling='$var_department_id' ORDER BY afdeling DESC";
$result = mysqli_query($dbc,$query);
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
//return(json_encode($data));
}
}
我用下面的代碼來調用上面的函數,它是在getcharts.php:
<?php
session_start();
include('../functions/datafunctions.php');
include('../config/db.php');
if(!isset($_SESSION['username'])) {
header('Location: ../login.php');
}
//Determine the function that should be called
$var_function_id = $_GET['funcid'];
//Set variables
$var_ziekenhuis_id = $_GET['hosp'];
$var_department_id = $_GET['dept'];
switch ($var_function_id) {
case "top_chart":
//Call function
get_topchart($dbc, $var_ziekenhuis_id, $var_department_id);
case "bottom_chart":
//New code here
}
?>
的$.getJSON
我用的是:
$.getJSON('functions/getcharts.php', {
"hosp":hospital,
"dept":department,
"funcid":functionid},
當我跳過整個getcharts.php並直接使用開關功能時,事情確實起作用。這代碼:
<?php
session_start();
include('../config/db.php');
//Controleer eerst of gebruiker succesvol is ingelogd:
if(!isset($_SESSION['username'])) {
header('Location: ../login.php');
}
//Determine the function that should be called
$var_function_id = $_GET['funcid'];
//Set variables
$var_hospital_id = $_GET['hosp'];
$var_department_id = $_GET['dept'];
switch ($var_function_id) {
case "top_chart":
//function get_topchart($dbc, $var_ziekenhuis_id, $var_department_id) {
if($var_department_id == "Not Entered") {
$query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' ORDER BY afdeling DESC";
$result = mysqli_query($dbc,$query);
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
//return(json_encode($data));
} else {
$query = "SELECT afdeling, ligduur as ld FROM datavoorbeeld WHERE ziekenhuis='$var_hospital_id' and afdeling='$var_department_id' ORDER BY afdeling DESC";
$result = mysqli_query($dbc,$query);
$data = array();
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
//return(json_encode($data));
}
//}
}
?>
的$getJSON
代碼在這種情況下是:
$.getJSON('functions/datafunctions.php', {
"hosp":hospital,
"dept":department,
"funcid":functionid},
我在做什麼錯?
非常感謝!我還發現在SQL語句中我使用了錯誤的變量。現在事情正在起作用。 – CostCare
@CostCare閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Saty
@CostCare,如果您覺得它解決了您的問題,請接受我的答案,謝謝! – LMS94