MyBB Community Forums

Full Version: Problems using $db->query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to create a comment system for files with the help of mybb's functions, but when I input the following:
PHP Code:
$cquery $db->query("
    SELECT *
    FROM comments
    WHERE set=$set
    ORDER BY id DESC
"
); 
I get mySQL error 1064:
Quote:MySQLi error: 1064
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 'set=downloads ORDER BY id' at line 1
Query: SELECT * FROM comments WHERE set=downloads ORDER BY id
any suggestions?
Try:

PHP Code:
$cquery $db->query("
    SELECT *
    FROM comments
    WHERE set='$set'
    ORDER BY id DESC
"
); 
Also, depending on where $set comes from, you may need to consider escaping it before sending it into the query, eg:
PHP Code:
$cquery $db->query("
    SELECT *
    FROM comments
    WHERE set='"
.$db->escape_string($set)."'
    ORDER BY id DESC
"
); 
$set is stated before I include comments.php, the file where this is located.

Trying $db->escape_string produces the same error.

Some quick trial-and-error shows that the problem is with the WHERE statement; removing it results in no errors.

Okay, changing the field from set to cid fixed it; apparently "set" was reserved for something else.
Oh yeah, I think you may need to place quotes around 'set'.

SET is a reserved word.
Reference URL's