This question already has an answer here:
code:
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);
}
Error :
Notice: Undefined variable: hn in C:\xampp\htdocs\...
Notice: Undefined variable: pid in C:\xampp\htdocs\...
Notice: Undefined variable: datereg in C:\xampp\htdocs\...
Notice: Undefined variable: prefix in C:\xampp\htdocs\...
Notice: Undefined variable: fname in C:\xampp\htdocs\...
Notice: Undefined variable: lname in C:\xampp\htdocs\...
Notice: Undefined variable: age in C:\xampp\htdocs\...
Notice: Undefined variable: sex in C:\xampp\htdocs\...
how to fix that?
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;
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.
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) ;
I would guess your query isn't running as expected and you are getting to the return line with undefined variables.
Also, the way you are doing the variable assignment, you would be overwriting the same variable with each loop iteration, so you wouldn't return the entire result set.
Finally, it seems odd to return a numerically-keyed result set instead of an associatively-keyed one. Consider naming only the fields needed in the SELECT and keeping the key assignments. So something like this:
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());
$return = array();
while ($row = pg_fetch_array($result)){
$return[] = $row;
}
return $return;
}
You might also consider opening a question about how to improve your query, is it is pretty heinous as it stands now.
It looks like you don't have any records that match your query, so you'd want to return an empty array (or null or something) if the number of rows == 0.
xamp i guess your using mysql.
mysql_fetch_array($result);
And make sure $result is not empty.
来源:https://stackoverflow.com/questions/22465645/php-how-to-fix-notice-undefined-variable