First off, the double quotes are there because concatenation is being used; the following two statements are equivalent:
$sql = "SELECT * FROM user WHERE id = '" . $q . "'";
$sql = "SELECT * FROM user WHERE id = '$q'";
That said, integers don't need to be quoted in SQL, so this would be fine:
$sql = "SELECT * FROM user WHERE id = $q";
If $q
wasn't sanitized yet and you don't wish to use prepared statements, you could use sprintf()
in the following manner:
$sql = sprintf("SELECT * FROM user WHERE id = %d", (int)$q);
In the case of strings I would go for prepared statements or make sure the variable is properly escaped (in case the deprecrated mysql_
functions are still used):
$sql = sprintf("SELECT * FROM user WHERE id = '%s'", mysql_real_escape_string($q));