-2

Can someone explain me what it's wrong with that part of code?

$count_query_v1 = 'COUNT user_id FROM  stiri WHERE user_id = '.$_SESSION["user"]["nume"].''; 
$answer = mysql_query($count_query_v1) or die(mysql_error()); 
echo $answer;

Return me that error:

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 'COUNT user_id FROM stiri WHERE user_id = John' at line 1

Andy G
  • 19,232
  • 5
  • 47
  • 69
Muzica Veche
  • 65
  • 2
  • 9

1 Answers1

4

You need to add a select, and some parenthesis. This should work:

SELECT count(user_id) FROM stiri WHERE user_id = '.$_SESSION["user"]["nume"]

Read the documentation about COUNT. Also, your script is vulnerable to SQL Injection. I recommend you to also read this answer on how to prevent it.

Community
  • 1
  • 1
alxgb
  • 543
  • 8
  • 18