DCV_Diego
DCV_Diego

Reputation: 3

You have an error in your SQL syntax; check the manual for the right syntax to use near 'AND `user_id` = 1'

I know there are a lot of questions like these out there, but none of them seem to work, I cannot find the error on my syntax!

mysql_query("SELECT `type` FROM `poststuff` WHERE `post_id` = 7 AND `user_id` = 1");

Any help will be awesome! (Btw, error is on the title) Full code (for you people -,-)

function update_post($post_id, $user_id) {
$result = mysql_result(mysql_query("SELECT `type` FROM `poststuff` WHERE `post_id` = $post_id AND `user_id` = $user_id"), 0) or die(mysql_error());
if ($result !== null) {
if ($result === "no" ) {
$actual_count = mysql_result(mysql_query("SELECT `$result` FROM `posts` WHERE `post_id` = $post_id"), 0);
mysql_query("UPDATE `posts` SET `$actual_count` = $actual_count+1 WHERE `post_id` = $post_id AND `type` = 'no'");
}

Upvotes: 0

Views: 616

Answers (1)

spencer7593
spencer7593

Reputation: 108370

There doesn't appear to be a problem in the SQL text that you posted. I recommend you verify that it's a valid white space character preceding the AND keyword.

Based on the error message, I suspect there's an invalid character preceding the AND keyword in the query.

This is the kind of error you would get if you ran a query that had a comma or some other non-whitespace character preceding the AND keyword.

For example, running this query:

SELECT `type` FROM `poststuff` WHERE `post_id` = 7,AND `user_id` = 1
                                                  ^

would result in an error like the one you are getting:

... error in your SQL syntax; ... near 'AND `user_id` = 1'

The same error would be reported if there wasn't a value after the equal sign, and only whitespace. For example the following query would return the same error:

SELECT `type` FROM `poststuff` WHERE `post_id` = AND `user_id` = 1
                                                ^

Upvotes: 2

Related Questions