PHP 如何修复注意:未定义的变量:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22465645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP How to fix Notice: Undefined variable:
提问by Beebrabie
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?
如何解决?
回答by mseifert
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;
回答by akr
Declare them before the while loop.
在 while 循环之前声明它们。
$hn = "";
$pid = "";
$datereg = "";
$prefix = "";
$fname = "";
$lname = "";
$age = "";
$sex = "";
You are getting the notice because the variables are declared and assigned inside the loop.
您收到通知是因为变量是在循环内声明和分配的。
回答by Zak
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
您应该在 while 循环之外初始化变量。在 while 循环之外,它们目前没有作用域。您只是依靠 php 的优点让值在循环外延续
$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) ;
回答by Mike Brant
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:
最后,返回数字键控结果集而不是关联键控结果集似乎很奇怪。考虑仅命名 SELECT 中需要的字段并保留键分配。所以像这样:
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.
您也可以考虑提出一个关于如何改进查询的问题,它现在是否非常令人发指。
回答by Dr. McKay
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.
看起来您没有任何与您的查询匹配的记录,因此如果行数 == 0,您希望返回一个空数组(或 null 或其他内容)。
回答by Ganu
xamp i guess your using mysql.
xamp 我猜你在使用 mysql。
mysql_fetch_array($result);
And make sure $result is not empty.
并确保 $result 不为空。

