PHP How to fix Notice: Undefined variable:

This question already has an answer here:

  • Reference - What does this error mean in PHP? 30 answers

  • Define the variables at the beginning of the function so if there are no records, the variables exist and you won't get the error. Check for null values in the returned array.

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

    You should initialize your variables outside the while loop. Outside the while loop, they currently have no scope. You are just relying on the good graces of php to let the values carry over outside the loop

               $hn = "";
               $pid = "";
               $datereg = "";
               $prefix = "";
               $fname = "";
               $lname = "";
               $age = "";
               $sex = "";
               while (...){}
    

    alternatively, it looks like you are just expecting a single row back. so you could just say

    $row = pg_fetch_array($result);
    if(!row) {
        return array();
    }
    $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) ;
    

    Declare them before the while loop.

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

    You are getting the notice because the variables are declared and assigned inside the loop.

    链接地址: http://www.djcxy.com/p/69392.html

    上一篇: 无法修改标题信息

    下一篇: PHP如何修复注意:未定义的变量: