0

I'm trying to do a custom sql query in Views for Drupal 7 and I have the following code in my views php, but I'm not sure if I'm doing this correctly.

<?php $xx = db_query('SELECT * FROM acl_node INNER JOIN acl_user ON acl_node.acl_id=acl_user.acl_id INNER JOIN users ON acl_user.uid=users.uid WHERE nid=201');

print_r($xx);
?>

After running that I get a big array, but none of the data in it. I tested the sql query and it is indeed working via my sql builder tool.

enter image description here

1

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.

  • if its a small snippet of php code of like 3 lines. i dont know if it makes sense to make an entire module for it. i can understand if it was a high profile multi million dollar site with a bunch of devs, then yes go do it the "proper" way. im just working with a small site with barely any traffic. – Patoshi パトシ Oct 28 '16 at 16:18
  • little confused with "entire module", its just a module, same code but organised :), using php in configuration has its implications which is independent to number of devs working – arpitr Oct 28 '16 at 16:40
0

Was missing the foreach loop that apparently activates the sql querying:

<?php $xx = db_query('SELECT * FROM acl_node INNER JOIN acl_user ON acl_node.acl_id=acl_user.acl_id INNER JOIN users ON acl_user.uid=users.uid WHERE nid=201');

foreach($xx as $item) {
  if($item->uid != 1) {
    print_r($item->name);
  }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.