我创建了一个功能,可以获得具有相同薪水的所有员工名单。但它只会返回第一行但我想要所有数据我该怎么做呢?
功能代码
function getEmployee($employee, $salary) {
$select = mysql_query("SELECT * FROM $employee where salary = '$$salary'");
if ($select) {
while ($row = mysql_fetch_array($select)) {
$employeeInfo = array(
'fname' => $row['fname'],
'mname' => $row['buyQty'],
'lname' => $row['lname'],
'employeeCode' => $row['employeeCode'],
'email' => $row['email'],
'mobile' => $row['mobile'],
);
return $employeeInfo;
}
} else {
echo mysql_error();
}
}
函数调用
$entry[] = getEmployee('abc', '10000');
echo "<pre>";
print_r(array_values($entry));
echo "</pre>";
谢谢
答案 0 :(得分:5)
尝试
while ($row = mysql_fetch_array($select)) {
$employeeInfo[] = array(
'fname' => $row['fname'],
'mname' => $row['buyQty'],
'lname' => $row['lname'],
'employeeCode' => $row['employeeCode'],
'email' => $row['email'],
'mobile' => $row['mobile'],
);
}
return $employeeInfo;
答案 1 :(得分:1)
请尝试
function getEmployee($employee, $salary) {
$select = mysql_query("SELECT * FROM $employee where salary = '$$salary'");
if ($select) {
$allEmployeesInfo = array();
while ($row = mysql_fetch_array($select)) {
$employeeInfo = array(
'fname' => $row['fname'],
'mname' => $row['buyQty'],
'lname' => $row['lname'],
'employeeCode' => $row['employeeCode'],
'email' => $row['email'],
'mobile' => $row['mobile'],
);
array_push($allEmployeesInfo, $employeeInfo);
}
return $allEmployeesInfo;
} else {
echo mysql_error();
}
}
答案 2 :(得分:1)
您只获得一个值(迭代中的最后一个数组),因为您实际上会在每次迭代时覆盖$ employeeInfo并且在循环终止时$ employeeInfo仅保留最后一个数组。 在getEmployee函数中将其设置为$ employeeInfo []之类的数组。