gpt4 book ai didi

php - mysql中表为空时出现错误,如何解决?

转载 作者:行者123 更新时间:2023-11-29 15:33:01 24 4
gpt4 key购买 nike

我在 php 中有查询

select  
c.*,
CONCAT(c.first_name, ' ' ,c.middle_name, ' ' ,c.last_name) as name,
r.paid_amount as total_amount_paid,
r.emi_date as emi_date_from_reciept,
ltc.loan_amount as total_remaining_loan_amount ,
ltc.emi_date as emi_loan_date,
ltc.no_of_month as num_of_months_from_ltc
FROM loan_to_customer ltc
LEFT JOIN customer c ON ltc.customer_id = c.customer_id
LEFT JOIN receipt r ON r.customer_id = c.customer_id
WHERE c.cust_mobile = '$cust_mobile' OR c.unique_no = '$unique_no'

我收到此错误

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1`

当我的表为空时,我收到此错误。

如果我在 phpmyadmin 上执行它,它就会运行并成功。

我的php代码是这样的


<?php

include "connection.php";



extract($_REQUEST);



$data = array();

$resArr = array();






$query = customSelectQuery("select c.*, CONCAT(c.first_name, ' ' ,c.middle_name, ' ' ,c.last_name) as name,r.paid_amount as total_amount_paid,
r.emi_date as emi_date_from_reciept, ltc.loan_amount as total_remaining_loan_amount ,
ltc.emi_date as emi_loan_date, ltc.no_of_month as num_of_months_from_ltc FROM loan_to_customer ltc
LEFT JOIN customer c ON ltc.customer_id = c.customer_id
LEFT JOIN receipt r ON r.customer_id = c.customer_id
WHERE c.cust_mobile = '$cust_mobile' OR c.unique_no = '$unique_no'");

if (isset($query)) {
$ltc_data = array();
foreach ($query as $row) {
$ltc_data = array(
'loan_amount' => $row['loan_amount'],
'total_remaining_loan_amount' => $row['total_remaining_loan_amount'],
'no_of_month' => $row['no_of_month'],
"num_of_months_from_ltc"=>$row['num_of_months_from_ltc'],

"emi_date_from_reciept" => $row['emi_date_from_reciept'],

"customer_id"=>$row['customer_id'],
);

}

}

$customer_id = $ltc_data['customer_id'];





$query1 = customSelectQuery("SELECT * FROM receipt WHERE customer_id = $customer_id");




$penalty_amoount = '100';




$paid_emi_date = array();

foreach ($query1 as $row1) {

$paid_emi_date[] = array('date'=>$row1['emi_date'],'amount'=>$row1['paid_amount'], 'penalty_amoount'=>$penalty_amoount);

}
$loan_amount = $ltc_data['loan_amount'];

$total_remaining_loan_amount = $ltc_data['total_remaining_loan_amount'];

$total_amount_paid = $loan_amount - $total_remaining_loan_amount;

$no_of_month = $ltc_data['no_of_month'];

$num_of_months_from_ltc = $ltc_data['num_of_months_from_ltc'];

$total_paid_emi_month = $no_of_month - $num_of_months_from_ltc;

$penalty_amoount1 = '20';

if (sizeOf($query) > 0) {

$d = array();

foreach ($query as $row) {

// $output = [];
foreach ( explode(',', $row['emi_loan_date']) as $date ) {
$output[] = ['date' => $date, 'emi_amount' => $row['emi_amount'], 'penalty_amoount'=>$penalty_amoount1];

}
$emi_date1 = $output[0]['date'];
$emi_a = $output[0]['emi_amount'];
$p_amo = $output[0]['penalty_amoount'];
$f_a = $emi_a + $p_amo;


$d[] = array(

"name" => $row['name'],
"Loan_Account_No" => $row['unique_no'],
"product_amount"=> $row['product_amount'],

"num_of_months" => $row['num_of_months'],

"no_of_month" => $no_of_month,

"loan_amount" => $row['loan_amount'],
"total_paid_emi_month" =>$total_paid_emi_month,
'total_amount_paid' => $total_amount_paid,
'total_remaining_loan_amount' => $row['total_remaining_loan_amount'],
"pending_emi_amount"=>$f_a,
"pending_emi_date"=>$emi_date1,
// "emi_date1" => explode(',', $row['emi_loan_date']),
"emi_date1" =>$output,
"paid_emi_date"=> $paid_emi_date,
"start_emi_date"=> $row['loan_date'],
"emi_amount"=> $row['emi_amount'],
// "emi_pending_amount"=>

);

}


}

if($d === null){

$d = " ";



$message = "not found loan data.";

}



$resArr = array("success" => 1, "data" => $d, "message" => $message);

header('Content-Type: application/json');

echo str_replace("\/", "/", json_encode($resArr, JSON_UNESCAPED_UNICODE));

?>


最佳答案

将“$cust_mobile”替换为“\”.$cust_mobile.'\'',将“$unique_no”替换为“\”.$unique_no.'\''。因为 $cust_mobile 和 $unique_no 是变量

select  c.*, CONCAT(c.first_name, ' ' ,c.middle_name, ' ' ,c.last_name) as name,r.paid_amount as total_amount_paid,
r.emi_date as emi_date_from_reciept, ltc.loan_amount as total_remaining_loan_amount ,
ltc.emi_date as emi_loan_date, ltc.no_of_month as num_of_months_from_ltc FROM loan_to_customer ltc
LEFT JOIN customer c ON ltc.customer_id = c.customer_id
LEFT JOIN receipt r ON r.customer_id = c.customer_id
WHERE c.cust_mobile = '\''.$cust_mobile.'\'' OR c.unique_no = '\''.$unique_no.'\''

关于php - mysql中表为空时出现错误,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58536899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com