Why use ` around table names? php/mysql
Moderator: General Moderators
Why use ` around table names? php/mysql
I can't seem to find out why some php scripts use an ` around mysql table names, also phpMyAdmin uses them with db dumps. I don't see them used in any tutorials either, can anyone enlighten me on this?
also maybe explain the difference to using a ` and not a ' 'single quote'?
I can't even find a name for them, ticks?
Thanks
also maybe explain the difference to using a ` and not a ' 'single quote'?
I can't even find a name for them, ticks?
Thanks
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Re: Why use ` around table names? php/mysql
Usually they are reffered to as `backticks`mikebr wrote: I can't even find a name for them, ticks?
mikebr wrote:I can't seem to find out why some php scripts use an ` around mysql table names, also phpMyAdmin uses them with db dumps. I don't see them used in any tutorials either, can anyone enlighten me on this?
MySQL Manual wrote: An identifier [table or field name, B.W.] may be quoted or unquoted. If an identifier is a reserved word or contains special characters, you must quote it whenever you refer to it. For a list of reserved words, see Section 9.6, “Treatment of Reserved Words in MySQL”. Special characters are those outside the set of alphanumeric characters from the current character set, ‘_’, and ‘$’.
The identifier quote character is the backtick (‘`’):
Can you explain why I would need to do this?And when inserting, selecting etc you would need to make sure you always enclose the names in backticks.
I have read the manual link as 'Weirdan' posted above, and it states:
The identifier quote character is the backtick (‘`’):
andAn identifier may be quoted or unquoted. If an identifier is a reserved word or contains special characters, you must quote it whenever you refer to it.
I am still a bit confused as to 'And when inserting, selecting etc you would need to make sure you always enclose the names in backticks.'. I also read somewhere that a scripter had to use back ticks or his server would not run the query, can this be right?The use of identifier quote characters in identifiers is permitted, although it is best to avoid doing so if possible.
Thanks again
He meant that this is true when you are using a keyword as a fieldname.And when inserting, selecting etc you would need to make sure you always enclose the names in backticks.
It was me that said my host required me to use backticks...well, that isn't entirely true.
A query such as...
Code: Select all
SELECT * FROM users WHERE id = '1'but a more complex query such as this requires the backticks, otherwise it returns a syntax error
Code: Select all
SELECT `q`.`id` as `mainID`, `q`.`qgid`, `q`.`section`, `q`.`fieldType`, `a`.`qid`, `a`.`uid`, `a`.`answer` as `mainAnswer`
FROM `gradlife_co_uk_-_gradlife`.`questions` as `q`, `gradlife_co_uk_-_gradlife`.`answers` as `a`
INNER JOIN `gradlife_co_uk_-_gradlife`.`answers` ON `q`.`id` = `a`.`qid`
WHERE `q`.`section` = '2' AND `a`.`uid` = '".$userID."'
GROUP BY `q`.`id`"Thanks for the info on this from everyone, just one more question and I think that will tie it all up:
Does it need to be back ticks? would single quotes be ok, in the manual it says:
Thanks again
Does it need to be back ticks? would single quotes be ok, in the manual it says:
but does this mean for the purposes of their examples or the back tick must be used as the quote? the reason I ask is because I have seen single quotes being used also for this purpose.The identifier quote character is the backtick (‘`’):
Thanks again
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
It has to be backticks in MySQL at least... I've seen square brackets [table_name] used before (some other DB no doubt) but I can't remember wheremikebr wrote:Thanks for the info on this from everyone, just one more question and I think that will tie it all up:
Does it need to be back ticks? would single quotes be ok, in the manual it says:
but does this mean for the purposes of their examples or the back tick must be used as the quote? the reason I ask is because I have seen single quotes being used also for this purpose.The identifier quote character is the backtick (‘`’):
Thanks again
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
single quotes - ' ' - for quoting string datamikebr wrote:but does this mean for the purposes of their examples or the back tick must be used as the quote? the reason I ask is because I have seen single quotes being used also for this purpose.
pair o' backticks - ` ` - for quoting db/tbl/col names...
understood
in Access/SQL Serverd11wtq wrote:I've seen square brackets [table_name] used before (some other DB no doubt) but I can't remember where
I wrote:In Access/SQL Server the same is acheived by surrounding them with box brackets - like this [db/table/column name].