Please have a try and you will never see the error
$rows = array();
/*while($row = db_fetch_object($result)) {
$rows[] = $row;
}*/
foreach ($result as $row) {
$rows[] = $row;
return $rows;
Please also try to change those:
$result = db_query("select * from {files} where fid = %d", $val['_fid']);
while ($data = db_fetch_object($result)) {
$thefile = $data;
}
to
$query = db_select('files', 'f')
->fields('f')
->condition('fid', $val['_fid']);
$thefile = $query->execute()->fetchObject();
?