有人可以帮我解决这个问题,我收到此错误消息提示:未定义的变量:选择,我理解错误,但我没有看到问题。
public function getEmployeesArray($conn)
{
// $conn =
// $conn = $this->get('database_connection');
$employees = $conn->fetchall('Select * from vEmployee order by emp_lastname');
foreach ($employees as $emp_row) {
$choices[$emp_row['employee_id']] = $emp_row['emp_lastname'] . ', ' . $emp_row['emp_firstname'];
}
return $choices;
}
答案 0 :(得分:3)
$choices
尝试此操作之前,是foreach
未定义:
public function getEmployeesArray($conn) {
// $conn =
// $conn = $this->get('database_connection');
$employees = $conn->fetchall('Select * from vEmployee order by emp_lastname');
$choices = [];
foreach ($employees as $emp_row) {
$choices[$emp_row['employee_id']] = $emp_row['emp_lastname'] . ', ' . $emp_row['emp_firstname'];
}
return $choices;
}
答案 1 :(得分:2)
试试这个:
public function getEmployeesArray($conn) {
$choices = []; //or what you want
$employees = $conn->fetchall('Select * from vEmployee order by emp_lastname');
foreach ($employees as $emp_row) {
$choices[$emp_row['employee_id']] = $emp_row['emp_lastname'] . ', ' . $emp_row['emp_firstname'];
}
return $choices;
}
您需要初始化$choices
,因为如果没有$employees
它从未设置过。
现在的问题是您的查询没有返回任何值,因此您的代码无法进入您的foreach循环