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

huangapple go评论71阅读模式
英文:

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=&quot;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=&#39;&quot;.$fid.&quot;&#39;
                        and fs.force_id=fd.force_id and fd.next_review_date&lt;&gt;&#39;1970-01-01 00:00:00&#39;
                     order by shape_date desc limit 1&quot;;
$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[&#39;next_review_date&#39;] == &quot;&quot;) // line no 362 where error is showing
{
    $s_m_shape_2[&#39;next_review_date&#39;] = &#39;0&#39;;
}
if($med_shape_1&gt;&#39;0&#39; or $med_shape_2&gt;&#39;0&#39;)
{
							
    if($s_med_cat[&#39;shape_date&#39;]&gt;$s_m_shape_2[&#39;next_review_date&#39;])
    {
								
        $ind_med_shape=$s_med_cat[&#39;code&#39;].&quot; As On &quot;;
        $ind_med_shape_date=date(&quot;d-m-Y&quot;,strtotime($s_med_cat[&#39;shape_date&#39;]));
    }

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=&#39;&quot;.$fid.&quot;&#39;:我假设没有一行的force_id等于变量*$fid*中包含的值。我建议您在将其传递给查询之前检查$fid的值(例如使用var_dump)。
此外,通过字符串连接组合查询会导致可能的SQL注入风险,最好使用预处理语句。

这可以改进您的代码:

// 带有连接的查询
$sql_med_shape_2 = &lt;&lt;&lt;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 &lt;&gt; &#39;1970-01-01 00:00:00&#39;
    ORDER BY shape_date DESC
    LIMIT 1
SQL;	

// ----- 调试$fid内容
// echo &#39;&lt;pre&gt;
// 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, &#39;s&#39;, $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 &gt; 0) {
	
	$s_m_shape_2[&#39;next_review_date&#39;] = $s_m_shape_2[&#39;next_review_date&#39;] ?: 0;  // &#39;previous&#39; 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=&#39;&quot;.$fid.&quot;&#39;: 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 = &lt;&lt;&lt;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 &lt;&gt; &#39;1970-01-01 00:00:00&#39;
    ORDER BY shape_date DESC
    LIMIT 1
SQL;	

// ----- debug $fid content
// echo &#39;&lt;pre&gt;
// 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, &#39;s&#39;, $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&#39;s test BEFORE if we have any result row
if ($med_shape_2 &gt; 0) {
	
	$s_m_shape_2[&#39;next_review_date&#39;] = $s_m_shape_2[&#39;next_review_date&#39;] ?: 0;  // &#39;previous&#39; line no 362
	
	//....

huangapple
  • 本文由 发表于 2023年4月17日 13:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定