Hello,

I'm pretty new to Drupal and I would really appreciate some help with SQL syntax errors that I'm getting after enabling any node access module.

I have experienced this in 6.13 and 6.14 (I recently upgraded to 6.14). The errors are of the form:

"user warning: 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"...

An example of one of the SQL queries that is breaking is:

SELECT DISTINCT(n.nid) 
FROM node n 
WHERE n.status = 1 
AND n.nid IN (4, 5) 
INNER JOIN node_access na ON na.nid = n.nid 
WHERE (na.grant_view >= 1 
AND ((na.gid = 0 AND na.realm = 'all') 
OR (na.gid = 0 AND na.realm = 'example_author')))

The problem seems to be that there is a WHERE clause before an INNER JOIN (there is a second WHERE clause after the join, so I presume the first WHERE clause should be combined with that).

Here is a screengrab to show what this looks like on the page: http://twitpic.com/it2l5

These start occurring after enabling any of the following modules:
- Node Access
- Simple Access
- Content Access
- My own custom module following the example of node_access_example.module from the API reference (http://api.drupal.org/api/drupal/developer--examples--node_access_exampl...)

I get the errors at any node if I am an anonymous or non-admin user.

I've tried disabling all non-core modules apart from the node access module, without success (I have disabled: Webform, Views, CCK, Custom Page and Menu Per Role). If I try again with a fresh installation and the bare bones then I don't get this problem, so I know I must have somehow got the site into a strange state, but I would really like to be able to fix it rather than have to re-do everything from scratch (I guess I will have to do that next if this forum post doesn't reveal any answers though!). One difference to most people is that I am running PHP in Java using Quercus (running in Jetty) - however, I can't see any reason why that would be affecting the SQL queries.

I had a look into hacking database.info temporarily to better understand the problem. I had some short-lived success by modifying db_rewrite_sql to move the WHERE clause inside $query into $where. The problem I found with this was the difficulty in pulling out the WHERE clause on its own, especially when there are further complications such as ORDER BY. So I thought I should now give up hacking and ask the pros!

Any ideas?

Thank you in advance,

Peter

Comments

poshaughnessy’s picture

Allelujah, I got it fixed at last! The latest patch (#38) from this thread solved it: http://drupal.org/node/151910

I didn't take too much notice of that thread before as it is about solving subqueries, which I haven't needed. However, it seems that the new code also fixes problems with the previous pattern in db_rewrite_sql in database.inc (sorry, when I said database.info in my previous post i meant database.inc).

I'm not too familiar with the syntax, but from a little bit of debugging I could see that the previous code didn't match the pattern to the query, so it didn't rearrange the JOIN to come before the WHERE. I wonder if this may be to do with the brackets in the query (e.g. "IN (4, 5)") because it looks as though it might be assuming that brackets mean a subquery. If anyone can explain it properly I'd be interested to hear!