我正在学习PHP,并且似乎无法弄清楚循环是否可以在函数内运行。
我的代码不在函数内部时工作,但是当我将它包装在函数中时,我无法将其输出。
我尝试输入代理商名称($ jur)并从数据库中获取与该名称相关联的名称列表。
function jur($conn, $jur){
// SQL that runs the search in the data base for all employees associated with an agency
$EmployeeListSQL = "SELECT emp.NAME as NAME, jur.NAME as AGENCY FROM SMARTCM . dbo . EMPMAST as emp
inner join SMARTCM . dbo . EMP_TO_JURIS as empjur
on emp.UniqueKey = empjur.FK_EMP_KEY
inner join SMARTCM . dbo . JURISDICT as jur
on empjur.FK_JURIS_KEY = jur.UniqueKey
where jur.name = '$jur'";
$employeeListSET = sqlsrv_query($conn, $EmployeeListSQL);
// create an array with all the names fetched from the data base
while($employee = sqlsrv_fetch_array( $employeeListSET, SQLSRV_FETCH_ASSOC)){
$employeeListArray[] = $employee['NAME'];
}
// list all the array items
foreach($employeeListArray as $employee){
echo $employee . "<BR>";
}
}
// Try to call the function list
jur($conn, 'Girard');