Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importance of backtick around table name in MySQL query

Tags:

php

mysql

In MySQL queries, how important is it to put backticks around a table name. Does it have something to do with security? Are MySQL injection attacks possible through the table name if the table name is created dynamically in PHP based on user inputs?

like image 765
Brian Avatar asked Dec 26 '09 08:12

Brian


2 Answers

The backticks help you from accidentally using a name that is a reserved word in SQL for example. Take a table named "where", it's a stupid name for a table I agree, but if you wrap it in backticks it will work fine.

like image 106
Bjorn Avatar answered Oct 18 '22 12:10

Bjorn


In MySQL queries, how important is it to put backticks around a table name. Does it have something to do with security?

As far as backticks are concerned, I use them when there is name conflict between mysql-specifcs names and those from query.

Are MySQL injection attacks possible through the table name if the table name is created dynamically in PHP based on user inputs?

When ever there is a user input, you need to make sure that you filter and validate the input coming from the user. So yes there is security risk to it.

I would recommend you to use intval for numbers and mysql_real_escape_string function for any variables that you may use in your queries.

like image 36
Sarfraz Avatar answered Oct 18 '22 12:10

Sarfraz