To elaborate a little further, it depends on the type of data being held by the variable. The example given above is perfect for a text value, but it will not work for a numeric value. It's important to think of how MySql needs the value, when writing a query into the console for example, literal strings are surrounded with single quotes, and numeric values are passed without any delimiters at all. A direct query would look like:
- Code: Select all
SELECT field1, field2 FROM table WHERE field1>0 and field2='example';
If we are using variables for the where values through php, we don't need the semi colon on the end, and the query is generated like this:
- Code: Select all
$field1=1;
$field2='example';
$sql="SELECT field1, field2 FROM table WHERE field1>" . $field1 . " and field2='" . $field2 . "'";
then you'd just call it with mysql_query($sql);