我试图从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"
}
我无法理解为什么会这样。提前致谢
答案 0 :(得分:0)
谢谢大家。我得到了答案。我试图获取数据没有fetch.now它清楚我。再次感谢。我正在学习php.here是我的代码
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;
}