Page 1 of 1

MySQL query with unacceptable word...help!

Posted: Thu Jul 21, 2005 11:12 am
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.

Posted: Thu Jul 21, 2005 11:19 am
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.

Posted: Thu Jul 21, 2005 11:30 am
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

Posted: Thu Jul 21, 2005 11:36 am
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

Posted: Thu Jul 21, 2005 6:35 pm
by timvw