Siyah
Siyah

Reputation: 2897

Error in SQL syntax leads to database error

This is the error I am getting:

[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 '' at line 1]

I don't get the error. I can't find it.

This is my code:

function my_bp_adminbar_notifications_menu() {
global $bp;

if ( !is_user_logged_in() )
    return false;

echo '<li id="top-notification">';
_e( 'Alerts', 'buddypress' );

if ( $notifications = bp_notifications_get_notifications_for_user( $bp->loggedin_user->id ) ) { ?>
    <span><?php echo count( $notifications ) ?></span>
<?php
}

if ( $notifications ) {
    $counter = 0;
    for ( $i = 0; $i < count($notifications); $i++ ) {
        $alt = ( 0 == $counter % 2 ) ? ' class="alt"' : ''; ?>

        <li<?php echo $alt ?>><?php echo $notifications[$i] ?></li>

        <?php $counter++;
    }
} else { ?>

What is going on here? I do not see a single quote that could be faulty.

Upvotes: 2

Views: 88

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562348

It could be that you are calling some functions that in turn run SQL queries. I see you are calling two functions: is_user_logged_in() which is a standard function in the WordPress code, and bp_notifications_get_notifications_for_user() which is part of BuddyPress, a WordPress plugin.

I would guess the BuddyPress code has a code bug. I did a search and found that they have a number of bug reports about SQL syntax errors, so I guess they don't test their code enough.

First, you should upgrade to the latest BuddyPress version to see if the bug has been fixed already. If the problem is still there, contact their support: https://buddypress.org/support/

Explaining the error you got:

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 '' at line 1

Syntax errors in MySQL report the part of the query following the point where the syntax parser got confused. For example, if you put an extra comma in a select-list:

SELECT a, b, c, FROM ...

The syntax error would report a problem near 'FROM' because that keyword is unexpected following a comma.

If you get a syntax error report near '' then that means the parser got confused as it reached the end of the SQL query, because it didn't find what it expected. This is caused by many types of mistakes, for example because the SQL failed to close a parenthesis or a quote, or used a clause keyword without a required argument.

Here are some possible examples of a syntax error that occurs at the end of a query:

SELECT * FROM mytable WHERE
SELECT * FROM mytable WHERE col =
SELECT * FROM mytable WHERE col = 'string
SELECT * FROM mytable WHERE (x OR y

Basically, any kind of "unfinished" syntax would cause this error at the end.

Upvotes: 1

Related Questions