如何从php文件中的数据库查询获取数组数据到ajax成功数据

如何从php文件中的数据库查询获取数组数据到ajax成功数据

问题描述:

i am trying to get the data which is in array, from an SQL query to the ajax success data. Here is the code i tried,

function adminchecksub(value1){
//alert('hi');
var value1=$('#adminsid').val();
//alert(value1);
if(value1==''){
alert('Please enter a Sub id');
}
else{
//alert(value1);
 $.ajax({

                                        type: 'POST',
                                        url: root_url + '/services/services.php?method=adminsubcheck',
                                        data: {value1:value1},
                                        async: true,
                                        success: function (data) {
                                                alert(data);

                                                if (data == 1) {
                                                        alert('inside');
                                                        //window.location.assign(rdurl+"?sid="+sid);
                                                        return true;
                                                } else {
                                                        //alert('does not exist!');
                                                       //alert('outside');
                                                        return false;
                                                }

                                        }
                                });
}

now in the php method which is requested from the above ajax call, i have to get the data return by this function which is returning an array

   function adminsubcheck($sid){
$subid=$sid['value1'];
$row = array();
//echo $subid;
$query = "SELECT Sub_id,Status,Sub_type FROM Sub WHERE Sub_id=$subid";        
        //echo $query;
        //echo $subid;
        $queryresult = mysql_query($query);
        $count  = mysql_num_rows($queryresult);
         //echo $count;

         while ($r = mysql_fetch_assoc($queryresult)) {

           $row[] = $r;
        } 
        return $row;

}

here $row is returning an array as data to the ajax function, where i need to get all the items seperately. Some one help me out to get the values into the ajax call. Thanks in advance..

In your PHP file, you can return your array as a json object in the following way (notice how the PHP file echos the result to pass it back to javascript)

...
$json = json_encode($row);
echo $json

Now add the following to your javascrip ajax

 $.ajax({

...
 // Whatever code you currently have
...

}).done(function ( json ) {

    var data = JSON.parse(json);

    //Continue with javascript
});

You can handle the java script variable 'data' as an associative array like it was in PHP

also, look how you populate the php variable $row and adjust the following way:

$cnt = 0;
while ($r = mysql_fetch_assoc($queryresult)) {

        $row[$cnt] = $r;
        $cnt++;

} 
 $json = json_encode($row);
    echo $json;

in javascript you can access your rows the following way in teh data variable:

var value = data[0]['field_name'];

in the above statement, 0 will correspond to the value of $cnt (i.e. mysql returned row number)

return $row;

replace like this

echo json_encode($row);

and add dataType='json' in ajax like this

dataType: 'json',

If you want get the value in ajax call i think it shoul be :

    while ($r = mysql_fetch_assoc($queryresult)) {
       $row[] = $r;
    }

    echo json_encode(array('data'=>$row));
    exit;