skip to Main Content

MYSQL won’t let queries select rows when the column name is a mysql keyword in the where statement.

I’m creating a php api where it grabs a username that is attached to a key provided by the user but when it’s grabbing the username it can’t search for it using key == '$key' because “key” is a reserved MySQL word that the frontend generates (and it can’t be changed.)

I’ve tried surrounding the column name in “s, ‘s, `s, and []s, none of which have worked. If anyone could help me that would be great.

$key = $_GET['key'];

$sql = "SELECT * FROM discordlink WHERE key = '$key'";
$result = $conn->query($sql);

It should be able to find the row attached to the key using the where clause provided but it doesn’t becuase it’s a mysql reserved keyword.

2

Answers


  1. You can surround the column name with backticks(`) for the reserved mysql words

    SELECT `key` FROM `table`
    
    Login or Signup to reply.
  2. Bit late answer, but still there was no accepted answer I am adding this answer.

    We can give alias name for table and refer reserved keyword column in WHERE statement i.e

    SELECT * FROM discordlink AS d WHERE d.key = '$key'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search