Question

I have tried searching but didn't find a good answer matching my case.

I have a database structure managed by drupal. I wish to use that structure into another context on the same server making a php file that makes a sql query to the db and fetch the results into a page not managed by drupal (in this specific case drupal is more like a user interface for the content manager than a real CMS for website design).

I've installed views and setup the options and the criteria to extract data from the fields i require for my php script.

The preview show me exactly the field i'm asking and the query is shown like this:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, 'node'       AS field_data_field_descrizione_node_entity_type, 'node' AS  field_data_field_web_node_entity_type, 'node' AS field_data_field_logo_node_entity_type
FROM 
{node} node
WHERE (( (node.type IN  ('collaborazioni')) ))
ORDER BY node_created DESC

I manage to make a connection to the exact same server and db with a php script but when i send the query i recive this mysql_error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL     server version for the right syntax to use near '} node WHERE (( (node.status = '1') AND     (node.type IN ('collaborazioni')) )) ' at line 3

I really don't know what's wrong, maybe you can help me figure this out? Thank you in advance for any help.

Was it helpful?

Solution

Drupal uses table aliases in the views preview query. It gets replaced before the actual query is sent to the database. The alias part is {node}, which points to the node table in database, but adds the table prefix that you entered during site installation. If you did not use a database prefix then just remove the {node} part of the query. If you did, then you should replace the {node} with prefix_node.

So without prefix it should be:

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, 'node'       AS field_data_field_descrizione_node_entity_type, 'node' AS  field_data_field_web_node_entity_type, 'node' AS field_data_field_logo_node_entity_type
FROM 
node
WHERE (( (node.type IN  ('collaborazioni')) ))
ORDER BY node_created DESC

And with prefix it should be (replace the "prefix" part of the query with the prefix you used of course):

SELECT node.title AS node_title, node.nid AS nid, node.created AS node_created, 'node'       AS field_data_field_descrizione_node_entity_type, 'node' AS  field_data_field_web_node_entity_type, 'node' AS field_data_field_logo_node_entity_type
FROM 
prefix_node node
WHERE (( (node.type IN  ('collaborazioni')) ))
ORDER BY node_created DESC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top