Jump to content


Photo

Whoops - tablenames!


  • Please log in to reply
4 replies to this topic

#1 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 01 March 2006 - 05:04 PM

Hi all

I'm not new to php/mysql, but have pretty much been working as I go along.

I came across a problem today when i wanted to name one of my tables 'references'. I noticed that phpmyadmin was dealing with it fine, and my scripts were causing all sorts of trouble.

The only difference between my scripts and phpmyadmin was that phpmyadmin was enclosing table and fieldnames with a ` mark (like a backwards apostrophe for anyone that can't see that)

So ... my question(s) ... is it good practice to do this to ALL my table names? does it matter apart from when a tablename may share it's name with a mysql function/operator?

Cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#2 jworisek

jworisek
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 01 March 2006 - 06:33 PM

the problem is that "References" is a reserved word in MySQL... you should never be using it as a table name or any name if possible... it will save you a lot of troubles later. But yes, you can use the backticks in order to use reserved words as names if you really want to.

[a href=\"http://dev.mysql.com/doc/refman/4.1/en/reserved-words.html\" target=\"_blank\"]http://dev.mysql.com/doc/refman/4.1/en/reserved-words.html[/a]

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 01 March 2006 - 07:47 PM

Agreed -- backticks is just MySQL's way of escaping strings into literals so that they don't get interpreted as keywords. But I guarantee you that you'll run into many problems later on down the line. Get into the habit of avoiding such keywords. You can find the list [a href=\"http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html\" target=\"_blank\"]here[/a].
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 01 March 2006 - 07:59 PM

Using backticks all the time is almost always unnecessary and very cumbersome. Just avoid reserved words.

#5 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 02 March 2006 - 01:22 PM

spot on, thanks guys. unfortunately i've had to do the backtick workaround, as it was the difference between changing several hundred parts one by one or changing just a small section of code.

but definitely one i'll keep in mind for future projects!

Cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users