MySQL query with unacceptable word...help!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

MySQL query with unacceptable word...help!

Post by bwv2 »

Hi,
I'm having trouble with a query and hope someone knows how to get around my problem. I have a database with information stored for each month of the year. My columns are named "jan, feb, mar,...,nov, dec". Trouble is that "dec" already has a meaning in mysql as a synonym for "DECIMAL". When I try to query "dec" I get a syntax error.

I know that I could simply rename the column, but that throws a small twig into the machine and I'd like to avoid doing as such. Is there any way of disguising the column name "dec" so that mysql doesn't think I mean "decimal"?

In case there's any confusion, here's a snippet of my code...

Code: Select all

$sql="SELECT oct,nov,dec FROM datatable WHERE id=$id";
$result= mysql_query($sql, $conn);
$row= mysql_select_row($result);
Thanks in advance.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

try this:

Code: Select all

$sql="SELECT `oct`,`nov`,`dec` FROM `datatable` WHERE `id`=$id";
*note: those are NOT quotes, they are the little back-tick on the tilde key.
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Post by bwv2 »

Thanks.

Is it a good idea to put these back-ticks on all queries, or will they be unnecessary? The reason I ask is because I'm currently debugging some query problems and I'm looking for some kind of lead in what the problem could be. What are the ticks for?

Thanks,
Brad
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

they're unnecessary for most fields. Some people make it a regular habit so it's easy to identifiy which words are table fields and which words are tables etc, but it's quite cumbersome to do.

I don't do it if that helps any :D
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Post Reply