Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php AJAX:不从数据库获取关联数组_Php_Jquery_Mysql_Ajax - Fatal编程技术网

Php AJAX:不从数据库获取关联数组

Php AJAX:不从数据库获取关联数组,php,jquery,mysql,ajax,Php,Jquery,Mysql,Ajax,我有两个html格式的组合框,将使用jQuery和PHP从数据库中动态填充。当其中一个组合框发生更改时,我调用jQuery,并将更改后的组合框的值发送到我的php页面,以便它从我的数据库中查找值 function valueChanged(department){ $.get("AdminAjax.php",$("#department").serializeArray(), function(data){ alert(data);//

我有两个html格式的组合框,将使用jQuery和PHP从数据库中动态填充。当其中一个组合框发生更改时,我调用jQuery,并将更改后的组合框的值发送到我的php页面,以便它从我的数据库中查找值

function valueChanged(department){

        $.get("AdminAjax.php",$("#department").serializeArray(),

        function(data){
            alert(data);//debug code that ouputs the returned value
            var i =0;
            for(i=0; i < data.length; i++){
                //create a string that will be appended into the second combo box.   
                var option = '<option value="' + data.Category[i] +               
                '">=' + data.Category[i] + '</option>';
                $("#category").append(option);
            }
        },"html" //debug for the return
        );
    }
功能值已更改(部门){
$.get(“AdminAjax.php”,$(“#department”).serializeArray(),
功能(数据){
警报(数据);//输出返回值的调试代码
var i=0;
对于(i=0;i
我知道组合框的值是通过php页面的反复试验传递的

<?php
$department = mysql_real_escape_string($_GET["department"]);
//$department is the value passed from the jQuery.

$categorySQL = "Select Category from pagedetails where Department = '$department'";
//get the values for the second combo box based on the department

$rs = mysql_query($categorySQL);//<---this is where it fails.
//I have echoed out $rs and $rowCategory after I have fetched it. 
//They return Resource #4 and Array respectively.

while($rowCategory = mysql_fetch_assoc($rs)){
//I am expecting multiple records to be returned.
   $json_out = json_encode($rowCategory);
}
echo $json_out;
?>

您的php中的echo是错误的,您需要使用或而不是
$。获取。每次while为true时,您都会重置
$json_out`变量。您应该将所有值保存到一个数组中,然后对其进行一次json_编码。这还将确保success中的json是有效的json

试试这个:

$json_out = array();
while($rowCategory = mysql_fetch_assoc($rs)){
//I am expecting multiple records to be returned.
   $json_out[] = $rowCategory;
}
echo json_encode($json_out);

您将在每行提取上覆盖
$json\u out

相反,请尝试:

$json_out = array();
while($rowCategory = mysql_fetch_assoc($rs)){
   $json_out[] = $rowCategory;
}
echo json_encode($json_out);

美元。阿贾克斯成功了。我还切换到在while循环中输出字符串,如“
”.$rowcontegory[“Department”]”。