I am using Aptana Studio 3 on a Mac to write queries to get data out of a MySQL DB. First I build the SQL query in the SQL box of PHPmyadmin, when it works I convert to php code in PHPmyadmin and paste into aptana and adjust the variables. I noticed that the ' from PHPmyadmin looks diferent to that simply typed in aptana and sometimes the query fails. I guess ts something to do with the the ASCII codes - can someone enlighten me? Below is code that does work followed by some that doesn't.
Code:
$result = mysql_query($sql = "SELECT * FROM `orders`
WHERE `takenAt`= '{$_POST['takenAt']}'
and `collectDate` BETWEEN '{$_POST['date1']}' AND '{$_POST['date2']}'
and `specialCare`= 'yes'
ORDER BY `collectDate`", $connection );
Code:
$result = mysql_query($sql = "SELECT * FROM `orders`
WHERE 'takenAt'= '{$_POST['takenAt']}'
and `collectDate` BETWEEN '{$_POST['date1']}' AND '{$_POST['date2']}'
and `specialCare`= 'yes'
ORDER BY `collectDate`", $connection );
__________________
A talent for speaking differently, rather than for arguing well, is the chief instrument of cultural change. Richard Rorty
`takenAt` is a field. That would compare the value in takenAt to the $_POST['takenAt'].
'takenAt' is a string. That would compare the string takenAt to the value of $_POST['takenAt'].
MySQL uses accent grave to mark field properties. This is only required if you have chosen a property name that is a part of the reserved words list.
I see, so I could leave the accent grave out where defining the field so long as I don't use reserved words as field names, and the query fails if I try to use it as a string identifier - teach me to use cut and paste so much!
thanks
__________________
A talent for speaking differently, rather than for arguing well, is the chief instrument of cultural change. Richard Rorty
You also need the accent grave (I call them "tick marks") if your field or table names contain illegal characters, such as spaces and hyphens. And. yes, there are many people who like to put spaces in field names.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
And. yes, there are many people who like to put spaces in field names.
but those who learn how to use databases properly will substitute an underscore for the space and so avoid all the issues spaces in field names introduces.
Sadly, there are many who create reports directly from a database and, because they want their column names to "look pretty", they put spaces and many other illegal characters in the field names (dashes, periods, octothorps, currency symbols, and more).
You would be amazed at some of the places I have seen this. In companies with DBAs who should know better but who take a short cut to make the boss happy.
On the rare occasions that I've done something similar (almost always I will instead produce the report via ASP/JSP/et al.), *at least* I have done it via the use of aliases, so that only in the final SELECT do I need to use crap like this.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.