繁体   English   中英

MySQL 在 PHP OOP 中的 AJAX 调用上返回 null

[英]MySQL return null on AJAX call in PHP OOP

我正在尝试从 AJAX 调用中从 MySQL 检索数据,这是我的代码。

当我从前端调用 AJAX 时,它将返回null 当我在 phpMyAdmin 中运行它时,相同的查询将起作用。

这是我的类文件

    class Bpay{

        private $db;
        private $fm;

        public function __construct(){
            $this->db = new Database();
            $this->fm = new Format();
        }

        public function getAllPaymentRecord($borderid,$month_of,$year_of){

            $border_id  = mysqli_real_escape_string($this->db->link, $borderid);
            $month      = mysqli_real_escape_string($this->db->link, $month_of);
            $year       = mysqli_real_escape_string($this->db->link, $year_of);

            $query = "SELECT * FROM mms_border_payable WHERE border_id = '".$border_id."' AND month_of = '".$month."' AND year_of =  '".$year."' ORDER BY id ASC";
            $result = $this->db->select($query);
            return $result;
        }
    }

这样的处理程序文件。 我在哪里发送 AJAX 请求并从类中获取 JSON 数据

    include 'Bpay.php';

    if(isset( $_POST['borderid'] )) {

        $recordArray = array();

        $regBpay = new Bpay();

        $borderid       = $_POST['borderid'];
        $month_of       = $_POST['month_of'];
        $year_of        = $_POST['year_of'];

        $recordArray['borderid'] = $borderid;
        $recordArray['month_of'] = $month_of;
        $recordArray['year_of'] = $year_of;

        $recordArray['paymentRecords'] = $regBpay->getAllPaymentRecord($borderid,$month_of,$year_of);

        header('Content-Type: application/json');
        echo json_encode($recordArray);
    }

这是我的 ajax 调用

    var borderid = $("#border").val();
    var month_of = $("#month_of").val();
    var year_of = $("#year_of").val();


    $.ajax({
        url: '../classes/recordHandler.php',
        dataType: "json",
        type: 'POST',
        async: false,
        cache: false,
        data: {
            borderid : borderid,
            month_of : month_of,
            year_of : year_of
        },
        success: function (data) {

            console.log(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        }
    });

得到这样的结果

    {
        borderid:"3"
        month_of:"2"
        paymentRecords:
            current_field:null
            field_count:null
            lengths:null
            num_rows:null
            type:null

        year_of:"2018"
    }

我不明白为什么会发生这种情况。 提前致谢

谢谢大家。 我得到了答案。 我试图在不获取数据的情况下获取数据。现在我很清楚。 再次感谢。我正在学习 php。这是我的代码

public function getAllPaymentRecord($borderid,$month_of,$year_of){

    $border_id  = mysqli_real_escape_string($this->db->link, $borderid);
    $month      = mysqli_real_escape_string($this->db->link, $month_of);
    $year       = mysqli_real_escape_string($this->db->link, $year_of);

    $query = "SELECT * FROM mms_border_payable WHERE border_id = '3' AND month_of = '".$month."' AND year_of =  '".$year."' ORDER BY id ASC";

    $result = $this->db->select($query);
    $resArray = array();

    if($result){
        while($data = $result->fetch_assoc()){
            array_push($resArray, $data);
        }
    }
    return $resArray;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM