Drupal 7 Execute a mysql sql query in Views using Views PHP
You must avoid using php in UI.

As a solution
  - Either you can override the views template.
  - Or you can try implmenting hook_views_pre_render.(Reference https://knackforge.com/blog/sivaji/how-alter-views-field-output-programmatically)

Now, about fetching the results from query result object.  

Other than iterating through the result set, there are other ways to fetch results.