How to use drupal variable in sql query in view

The drupal website path is http://domain/v1/v2/v3

and the varials in the path is arg(0)=v1, arg(1)=v2 and arg(2)=v3

So in your sql command in view, you can use like this:

$argument= arg(1);

$view--> build_info['query']="select * from table_name where name='$argument'";

Note:

1. there is no need to add schema name in front of the table name even the schema is not public

2. give alias name to each table and field

SELECT feature.feature_id AS feature_id,
    feature.uniquename as feature_uniquename
      FROM feature feature
      LEFT JOIN featureloc fl ON feature.feature_id = fl.feature_id
      LEFT JOIN feature srcf ON fl.srcfeature_id = srcf.feature_id
      WHERE srcf.name = '$argument'"

No comments:

Post a Comment

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...