php 从数据库中获取数据的值,但没有数据的值,因此从数据库返回空值...想要检查该值并继续

gjmwrych  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(140)
$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']));
    }

错误显示为:
/home/FTP/e_sangrahan/DataSearch/new_policy/personsdetails_new_policy.php LINE:362
第362章在上面……

dzjeubhm

dzjeubhm1#

正如@barmar所建议的,对第362行错误最合理的解释是查询没有产生任何结果。
最有可能失败的情况是:WHERE fs.force_id='".$fid."':我假设没有 force_id 等于变量 $fid 中包含的值的行。我建议您在将$fid的值传递给查询之前检查它(例如使用var_dump)。此外,使用字符串串联的查询组合可能会暴露出SQL注入,最好与预准备语句一起使用
这可能是你的代码的一个改进:

// 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
    
    //....

相关问题