Question

I can't get my NOT EXISTS mysql statement to work and it's driving me mad now:

$ancestors = mysql_query('
    SELECT * FROM comments e
        WHERE 
            ancestors = "' . $comment["id"] . '" AND 
            user_id != "' . $user->user_object["id"] . '" AND
                NOT EXISTS
                    (
                        SELECT  null 
                        FROM    notifications d
                        WHERE   d.target_id = e.id
                    )
', $database->connection_handle);

Any ideas?

ERROR:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /<>/<>/functions.php on line 785

Line 785:

    while($reply = mysql_fetch_array($ancestors, MYSQL_ASSOC)){

If I do this:

$ancestors = mysql_query('SELECT * FROM ' . $database->db_prefix . 'comments 
    WHERE    
        ancestors = "' . $comment["id"] . '" AND 
        user_id != "' . $user->user_object["id"] . '"', 
        $database->connection_handle
);

It returns my results i expect.

the notifications table does contain an entry

mysql var dump =

string(46) "Table 'whatever_co.comments' doesn't exist"

//SOLVED::: ' . $database->db_prefix . ' was missing from my table selectors.

Was it helpful?

Solution 3

I was missing my table prefix: ' . $database->db_prefix . ' from comments and notifications. The reason for the question was its the first time i have used not exists.. and was assuming that was the part that was incorrect. Thanks for all your time. This causes for celebration.

OTHER TIPS

Your NOT EXISTS is fine. I think it may be a problem with your select. Do you have a field called "null" in that table? If not that is your problem.

I think you are looking for something along the lines of

Select * 
FROM notifications d
WHERE d.target_id = e.id
AND e.id IS NULL

Try using something like that.

WHERE EXISTS and WHERE NOT EXISTS don't look at the contents of the rows being returned by the subquery. They simply check if ANY rows are returned: http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

So even though you're selecting a hard-coded null, which would normally "not exist", MySQL isn't looking at that null - it's looking at the row which contains that null, and evaluates it to "hey, that exists".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top