沃梦达 / 编程问答 / php问题 / 正文

如何在PHP中修复&q;注意:未定义的变量&q;?

How can I fix quot;Notice: Undefined variablequot; in PHP?(如何在PHP中修复q;注意:未定义的变量q;?)

本文介绍了如何在PHP中修复&q;注意:未定义的变量&q;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码:

Function ShowDataPatient($idURL)
{
    $query =" select * from cmu_list_insurance,cmu_home,cmu_patient where cmu_home.home_id = (select home_id from cmu_patient where patient_hn like '%$idURL%')
                     AND cmu_patient.patient_hn like '%$idURL%'
                     AND cmu_list_insurance.patient_id like (select patient_id from cmu_patient where patient_hn like '%$idURL%') ";

    $result = pg_query($query) or die('Query failed: ' . pg_last_error());

    while ($row = pg_fetch_array($result))
    {
        $hn = $row["patient_hn"];
        $pid = $row["patient_id"];
        $datereg = $row["patient_date_register"];
        $prefix = $row["patient_prefix"];
        $fname = $row["patient_fname"];
        $lname = $row["patient_lname"];
        $age = $row["patient_age"];
        $sex = $row["patient_sex"];
    }
    return array($hn, $pid, $datereg, $prefix, $fname, $lname, $age, $sex);
}

错误:

Notice: Undefined variable: hn in C:xampphtdocs...
Notice: Undefined variable: pid in C:xampphtdocs...
Notice: Undefined variable: datereg in C:xampphtdocs...
Notice: Undefined variable: prefix in C:xampphtdocs...
Notice: Undefined variable: fname in C:xampphtdocs...
Notice: Undefined variable: lname in C:xampphtdocs...
Notice: Undefined variable: age in C:xampphtdocs...
Notice: Undefined variable: sex in C:xampphtdocs...

我如何修复它?

推荐答案

在函数的开头定义变量,这样如果没有记录,变量就会存在,您就不会收到错误。检查返回的数组中是否有空值。

$hn = null;
$pid = null;
$datereg = null;
$prefix = null;
$fname = null;
$lname = null;
$age = null;
$sex = null;

这篇关于如何在PHP中修复&q;注意:未定义的变量&q;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在PHP中修复&q;注意:未定义的变量&q;?

基础教程推荐