Symfony中的未定义变量

时间:2017-08-22 10:46:54

标签: php

有人可以帮我解决这个问题,我收到此错误消息提示:未定义的变量:选择,我理解错误,但我没有看到问题。

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;
}

2 个答案:

答案 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循环