[英]MySQL return null on AJAX call in PHP OOP
I'm trying to retrieve data from MySQL from AJAX call here is my code.我正在尝试从 AJAX 调用中从 MySQL 检索数据,这是我的代码。
When I call AJAX from front-end then it will return null
.当我从前端调用 AJAX 时,它将返回
null
。 Same query will work when I run it in phpMyAdmin.当我在 phpMyAdmin 中运行它时,相同的查询将起作用。
here is my class file这是我的类文件
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;
}
}
Handler file like this.这样的处理程序文件。 Where I send my AJAX request and get JSON data from class
我在哪里发送 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);
}
here is my ajax call这是我的 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);
}
});
got result like this得到这样的结果
{
borderid:"3"
month_of:"2"
paymentRecords:
current_field:null
field_count:null
lengths:null
num_rows:null
type:null
year_of:"2018"
}
I can't understand why this happening.我不明白为什么会发生这种情况。 Thanks in advance
提前致谢
Thanks all.谢谢大家。 i got my answer.
我得到了答案。 i tried to get data without fetch.now its clear to me.
我试图在不获取数据的情况下获取数据。现在我很清楚。 thanks again.i'm learning php yet.here is my code
再次感谢。我正在学习 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.