3

Can someone help me figure out what i am doing wrong in my SELECT statement?

SELECT *
FROM group
WHERE login = 'admin'

My error is:

right syntax to use near 'group WHERE login = 'admin'' at line 1

It should fail but because of the fact that admin is not in the database but when I plug in data that is in the database it still fails.

JNK
  • 63,321
  • 15
  • 122
  • 138
Josh C.
  • 316
  • 1
  • 4
  • 14
  • Two quick hints: 1) SQL errors are triggered by MySQL, not PHP 2) Try not to strip the error code when you post the error message here. It provides valuable information as well. – Álvaro González Apr 15 '13 at 07:18

2 Answers2

11

GROUP is a reserved keyword. You need to escape it using back to avoid from syntax error.

SELECT * FROM `group` WHERE login = '$username'

If you have the privilege to alter the table name, change it to which it is not a reserved keyword to avoid from getting another syntax error again in the future. :D


As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Thank you! It has been awhile since I have worked with SQL so I had forgotten about keywords. I just changed the name of my table and now it works. I will look into the SQL injection stuff also – Josh C. Apr 15 '13 at 07:21
3

GROUP is a mysql reserverd keyword therefore it can be used, place it among backstick

`group`

Documentation here

Fabio
  • 23,183
  • 12
  • 55
  • 64