英文:
fatching the value of data from database but there is no value is present for data so return null value from DB ...want to check that value & proceed
问题
I'll provide the translation of the code and the error message:
$search_med_shape_2 = "select fs.shape_date, fs.shape_category, fd.next_review_date
from force_shape_2 fs, force_shape_2_detail fd
where fs.force_id='" . $fid . "'
and fs.force_id = fd.force_id and fd.next_review_date <> '1970-01-01 00:00:00'
order by shape_date desc limit 1";
$s_med_shape_2 = mysqli_query($connpis, $search_med_shape_2);
$s_m_shape_2 = mysqli_fetch_array($s_med_shape_2, MYSQLI_ASSOC);
$med_shape_1 = mysqli_num_rows($s_m_cat);
$med_shape_2 = mysqli_num_rows($s_med_shape_2);
if ($s_m_shape_2['next_review_date'] == "") // line no 362 where error is showing
{
$s_m_shape_2['next_review_date'] = '0';
}
if ($med_shape_1 > '0' or $med_shape_2 > '0')
{
if ($s_med_cat['shape_date'] > $s_m_shape_2['next_review_date'])
{
$ind_med_shape = $s_med_cat['code'] . " As On ";
$ind_med_shape_date = date("d-m-Y", strtotime($s_med_cat['shape_date']));
}
}
Error showing is:
"Trying to access array offset on value of type null in /home/FTP/e_sangrahan/DataSearch/new_policy/personsdetails_new_policy.php on line 362"
Line number 362 is commented above.
英文:
$search_med_shape_2="select fs.shape_date,fs.shape_category,fd.next_review_date
from force_shape_2 fs,force_shape_2_detail fd
where fs.force_id='".$fid."'
and fs.force_id=fd.force_id and fd.next_review_date<>'1970-01-01 00:00:00'
order by shape_date desc limit 1";
$s_med_shape_2=mysqli_query($connpis,$search_med_shape_2);
$s_m_shape_2=mysqli_fetch_array($s_med_shape_2,MYSQLI_ASSOC);
$med_shape_1=mysqli_num_rows($s_m_cat);
$med_shape_2=mysqli_num_rows($s_med_shape_2);
if($s_m_shape_2['next_review_date'] == "") // line no 362 where error is showing
{
$s_m_shape_2['next_review_date'] = '0';
}
if($med_shape_1>'0' or $med_shape_2>'0')
{
if($s_med_cat['shape_date']>$s_m_shape_2['next_review_date'])
{
$ind_med_shape=$s_med_cat['code']." As On ";
$ind_med_shape_date=date("d-m-Y",strtotime($s_med_cat['shape_date']));
}
error showing is:
>Trying to access array offset on value of type null in /home/FTP/e_sangrahan/DataSearch/new_policy/personsdetails_new_policy.php on line 362
line no 362 is commented above...
答案1
得分: 1
以下是您要翻译的内容:
根据@barmar的建议,线362处错误的最合理解释是查询未返回结果。
最有可能失败的条件是:WHERE fs.force_id='".$fid."'
:我假设没有一行的force_id等于变量*$fid*中包含的值。我建议您在将其传递给查询之前检查$fid的值(例如使用var_dump)。
此外,通过字符串连接组合查询会导致可能的SQL注入风险,最好使用预处理语句。
这可以改进您的代码:
// 带有连接的查询
$sql_med_shape_2 = <<<SQL
SELECT fs.shape_date, fs.shape_category, fd.next_review_date
FROM force_shape_2 fs
INNER JOIN force_shape_2_detail fd ON fs.force_id = fd.force_id
WHERE fs.force_id = ?
AND fd.next_review_date <> '1970-01-01 00:00:00'
ORDER BY shape_date DESC
LIMIT 1
SQL;
// ----- 调试$fid内容
// echo '<pre>
// var_dump ($fid);
// die
// 预处理语句
$stmt_med_shape_2 = mysqli_stmt_init($connpis);
mysqli_stmt_prepare($stmt_med_shape_2, $sql_med_shape_2);
mysqli_stmt_bind_param($stmt_med_shape_2, 's', $fid);
mysqli_stmt_execute($stmt_med_shape_2);
$result_med_shape_2 = mysqli_stmt_get_result($stmt_med_shape_2);
$s_m_shape_2 = mysqli_fetch_array($result_med_shape_2, MYSQLI_ASSOC);
$med_shape_2 = mysqli_num_rows($result_med_shape_2);
// 在获取任何结果行之前测试一下
if ($med_shape_2 > 0) {
$s_m_shape_2['next_review_date'] = $s_m_shape_2['next_review_date'] ?: 0; // 'previous' line no 362
//....
英文:
As @barmar suggested, the most plausible explanation for the error at line 362 is that the query produces no results.
The condition that could most likely fail is: WHERE fs.force_id='".$fid."'
: I assume that there is no row with force_id equal to the value contained in the variable $fid. I advise you to inspect the value of $fid (for example with var_dump) before passing it to the query.
Furthermore, the composition of the query with a concatenation of strings exposes to a possible SQL injection, it would be better to use with prepared statements
This could be an improvement of your code:
// query with joins
$sql_med_shape_2 = <<<SQL
SELECT fs.shape_date, fs.shape_category, fd.next_review_date
FROM force_shape_2 fs
INNER JOIN force_shape_2_detail fd ON fs.force_id = fd.force_id
WHERE fs.force_id = ?
AND fd.next_review_date <> '1970-01-01 00:00:00'
ORDER BY shape_date DESC
LIMIT 1
SQL;
// ----- debug $fid content
// echo '<pre>
// var_dump ($fid);
// die
// prepared statement
$stmt_med_shape_2 = mysqli_stmt_init($connpis);
mysqli_stmt_prepare($stmt_med_shape_2, $sql_med_shape_2);
mysqli_stmt_bind_param($stmt_med_shape_2, 's', $fid);
mysqli_stmt_execute($stmt_med_shape_2);
$result_med_shape_2 = mysqli_stmt_get_result($stmt_med_shape_2);
$s_m_shape_2 = mysqli_fetch_array($result_med_shape_2, MYSQLI_ASSOC);
$med_shape_2 = mysqli_num_rows($result_med_shape_2);
// let's test BEFORE if we have any result row
if ($med_shape_2 > 0) {
$s_m_shape_2['next_review_date'] = $s_m_shape_2['next_review_date'] ?: 0; // 'previous' line no 362
//....
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论