Detailed thread here about the discovery of the issue: http://drupal.org/node/205653

In a nutshell, I started getting this warning on a site:

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 'INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((n' at line 1 query: INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all'))) LIMIT 0, 10 in D:\muun_nobackup\classiccars\includes\database.mysqli.inc on line 151.

Using the Devel module, I traced it to the views_build_view function, which was submitted a malformed query to node_db_rewrite_query. Result from Devel is here:

views_build_view
INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all'))) LIMIT 0, 10 

Turned out to be a block one of my views is creating - for some reason the views module is sending a null query when trying to build that block, which causes the warning and the malformed SQL. Unfortunately, because this was a while ago and I've only just reviewed the note reminding me to submit this as an issue, I can't remember the precise nature of the block causing this behaviour. =(

Hope this is enough to go on!

Comments

merlinofchaos’s picture

Hmm. NULL queries are supposed to be skipped (They return the empty text) and not actually run. That's very odd that something is getting through. I don't know why this might be.

greg.harvey’s picture

Hmm, indeed. Sorry I can't remember more detail about the specifics of the block. Therein lies the key, I suspect. =(

If I remember, I'll flesh this out.

jfall’s picture

I got the same warning (was so relieved to see this already posted)... Thanks for "talking to yourself" on the forum greg - it was very helpful in getting this tracked down a little further.

I isolated the View that caused the problem, and it only occurs when I add an Argument. I have plenty of views with arguments, but only this one seems to cause the SQL warning. The Argument is Node:ID and this is the only View where I've tried using nid as an argument. As soon as I remove the argument, SQL warnings stop.

Oddest thing is, so far as I can tell, the view block appears to work fine, and the SQL warning is given on pages where the block is not even showing. Hope that helps.

Here is a dump of the view (in case that's useful). It is basically pulling some of the content fields from the current node to display in a little sidebar block (http://drupal.org/node/226032). Note: the argument handling code does not seem to make a difference - it is the presence of the argument itself that causes the warning.

** EDIT: Dump Deleted - See below **

jfall’s picture

Status: Active » Fixed

Here you go Greg - I'm pretty sure I've got this one...

I simply re-built the permissions (at Admin >> Content Management >> Post Settings, oddly enough) and all these SQL errors went away (along with some other strange behavior that started to crop up when I was digging into the problem deeper).

If that doesn't work for you, re-open this issue - I was messing with a lot of stuff, but it did seem like the perm. rebuild did it. I owe MoC a few, so I hope I've closed this one for him.

osopolar’s picture

Status: Fixed » Postponed (maintainer needs more info)

I have the same problem!

The devel module is saying that there was that query:

INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 1 AND na.realm = 'forum_access'))) LIMIT 0, 100

Therefore watchdog says: You have an error in your SQL syntax; check the manual ....

I rebuilt the permissions - but the problem is still present. The query is not complete ... there is missing the select part. I was searching for the bad query with google if somebody has a solution. But I only found sites with the same problem. Just try:

http://www.google.com/search?q=near+INNER+JOIN+node_access+na+ON+na.nid+%3D+node.nid+WHERE+(na.grant_view+%3E+views

So I started to debug. I have a block-view. This view has an argument. For this argument I wrote an argument handling code which I put in template.php. In the case where the error occurred the argumenthandling code is returning nothing.
At this point I started to debug:
in line 567 in views.module we get back the bad query by calling:

  $query = db_rewrite_sql($info['query'], 'node');

$info['query'] is an empty string.

_db_rewrite_sql returns (to database.inc line265) :

$join = INNER JOIN {node_access} na ON na.nid = node.nid
$where = (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 1 AND na.realm = 'forum_access')))
$distinct = true

so we dive in line 271 in: if (!empty($where) || !empty($join)) { ...

we are building: $insert = $new = " join WHERE $where ";

and later we append this to the empty query-string:

  $query .= $insert;

there is no $replace set and so we return the $query which is not anymore empty. But dont have a valid sql-query:
in my case:

 INNER JOIN {node_access} na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 1 AND na.realm = 'forum_access')))  INNER JOIN {node_access} na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 1 AND na.realm = 'forum_access'))) 

I hope this will help for fixing. I don't know enough about what is going on, if it is enough to do:

else {
  if(!empty($query)) {
    $query .= $insert;
  }
}

Thats why I'd like that sombody will solve this how knows what's going on ... maybe the problem is somewhere else. Thank you.

greg.harvey’s picture

hmm, this has marched on a bit. Good posts, guys. Hopefully more for Merlin to go on, if he has time! He's probably up to his neck in porting to Drupal 6 though. =(

jfall’s picture

I can report this error no longer appears since re-build of permissions.
Was easily repeatable before - have not seen it since...
Good luck all in tracking it down.

tamago’s picture

I have the same/similar problem:

I try to dig deeper into it, so I just share my "experiences" in a rush.

as far as I know now:

  • It does not matter if "block" or "page".
  • Logged in as admin (uid ==1), I see no problems.
  • I inserted an expection to be thrown in case of an error (database.mysqli.inc - _db_query - line 151). The result is:
    Uncaught exception 'Exception' with message 'DB' in /var/www/dieligen/includes/database.mysqli.inc:151
    Stack trace:
    #0 includes/database.inc(201): _db_query(' INNER JOIN nod...', 0)
    #1 includes/pager.inc(72): db_query(' INNER JOIN {no...', Array)
    #2 modules/views/views.module(573): pager_query(' INNER JOIN {no...', '10', 0, ' INNER JOIN {no...', NULL)
    #3 modules/views/views.module(412): views_build_view('page', Object(stdClass), Array, '1', '10')
    #4 [internal function]: views_view_page('refs2ThisVideo')
    #5 includes/menu.inc(418): call_user_func_array('views_view_page', Array)
    #6 index.php(15): menu_execute_active_handler()
    #7 {main}
    
  • mysql.log shows in case of error:
    [...]
                        154 Query       SELECT acl_id FROM acl_user WHERE uid = 0
                        154 Query       SELECT acl_id FROM acl_user WHERE uid = 0
                        154 Query       INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 1 AND na.realm = 'content_access_rid')))
    [...]
    
  • mysql.log shows in case of no error:
                        266 Query       SELECT acl_id FROM acl_user WHERE uid = 0
                        266 Query       SELECT  DISTINCT(n.nid), n.title, l.comment_count, l.last_comment_timestamp FROM node n INNER JOIN node_comment_statistics l ON n.nid = l.nid  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 = 1 AND na.realm= 'content_access_rid'))) AND ( n.status = 1 AND n.type = 'forum' ) ORDER BY l.last_comment_timestamp DESC LIMIT 0, 5
    

I hope this helps a little bit. I try to follow the beast's trace now... ;-)

BTW, ThX for this module.

tamago

tamago’s picture

Much ado about nothing:
I just acknowledge what ositoblanco already found out.

From my point of view it seems to be a bug in db_rewrite_sql.
I think it's dangerous allowing to return a malfunction whatever-query when you have an empty query as the first parameter, just because _db_rewrite_sql returns values for $join and $where.

If the query-parameter is empty, it should just return this empty query. DOT.

I doubt that the views module is the only victim of this. But according to the agreement "don't change the core by yourself because of future patches" maybe the views module just should take care of this in the meantime.

views/views.module - line 567
original:
db_rewrite_sql($info['query'], 'node');

my proposal:
$query = ($info['query'])?db_rewrite_sql($info['query'], 'node'):'';

what do you think?

webchick’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new907 bytes

Thanks, tamago! That definitely seems to have fixed the problem here. A node permission rebuild did not.

Here's a patch.

jannalexx’s picture

Status: Reviewed & tested by the community » Needs review

this one fixes this for me also! (I hope). I cannot imagine how this error came up!

devel module log:
views_build_view INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all'))) LIMIT 0, 5

error:
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 'INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((n' at line 1 query: views_build_view INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all'))) LIMIT 0, 5 in D:\...\includes\database.mysql.inc on line 172.

thanks

webchick’s picture

Status: Needs review » Reviewed & tested by the community

Thanks for testing!

smk-ka’s picture

This patch requires an if less :)
(But I'll leave it up to you to mark either one as dupe, both fix the bug perfectly).

sun’s picture

Status: Needs review » Fixed

Thanks, committed #247420: SQL error when using empty text as argument default, since that one was cleaner.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.