Sahil Buddhadev Sahil Buddhadev - 5 months ago 25
MySQL Question

PHP Function with array return type

I have created a function which gets all employee list who has the same salary. But it will return only the first row but I want all the data how can I do it ?

Function code

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


Function Call

$entry[] = getEmployee('abc', '10000');
echo "<pre>";
print_r(array_values($entry));
echo "</pre>";


Thank You

Answer

Try

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;