I'm trying to build a query in a script that relies upon an object's attributes in order to retrieve the correct information. What I have is this:
$query = "SELECT fields FROM table WHERE fieldA = $this->x";
//Processing of results here
I've seen queries like this used before with string variables but I'm not sure if the rules are different if you're using a variable that you know has a numerical value and the corresponding column for said value is declared as an integer or decimal. Would I need to include single quotes around $this->x?
SELECT ... WHERE foo=1is valid, no need for single quotes (though MySQL's implicit type casting could handlefoo='1'). So, if you're absolutely certain $this->x contains only digits (always) you can do that,"SELECT fields FROM table WHERE fieldA = {$this->x}". On the other hand, why bother when there are almost fool-proof prepared statements+named parameters? ;-)