The code below could be more optimized right?
Possibly, but I suspect that it is not correct to begin with.
Consider that you are retrieving a maximum of 5 rows from the table. As such, ending the table row at the fifth column means that the table has exactly one row. If so, it makes no sense to start another row. Not only that, but the logic to handle an incomplete (or complete, for that matter) final row is missing.
Aside from that, $reccnt is not used. Also, instead of initialising $temp_friendids_arr to NULL, you might as well initialise it to an empty array, i.e., array(). You would no longer need to use is_array() to check, but instead can rely on the fact that the loop simply would run zero times if the array is empty. You do not use $ind in the foreach loop, so you might as well write it as foreach ($temp_friendids_arr as $val).
One last note: as far as I know, PHP does not have named arguments. As such,
show_user_photo($val,$photo_type='small',$user_name='yes')
is more likely:
show_user_photo($val, 'small', 'yes');