I am trying to create a query string based on GET values passed to common vars:
if isset, gTipo = $_GET['tipo'] and others like this.
So, here is the code that is not working:
$sqlLista = 'SELECT * FROM produtos';
if($gTipo <> 0 || $gLinha <> 0)
{
if($gtipo <> 0 && $gLinha == 0 )
{
$sqlLista .= ' WHERE id_tipo = '.$gTipo.'';
}
if($gtipo <> 0 && $gLinha <> 0)
{
$sqlLista .= ' WHERE id_tipo = '.$gTipo.' AND id_linha = '.$gLinha.'';
}
if($gTipo == 0 && $gLinha <> 0)
{
$sqlLista .= ' WHERE id_linha = '.$gLinha.'';
}
}
If i set my url as ?tipo=2&linha=4 , my script capture this GET vars and create the common var gTipo and gLinha. If any of this GET are not set, the gTipo or gLinha receive the '0' (zero) value.
When I run the script of query building, nothing is concatened to $sqlLista, except what is done outside the if ( $sqlLista = 'SELECT * FROM produtos'; ).
I am sure this must be a stupid thing that I cannot see. Please, help me =)
$gLinhaand$gtipovariables against SQL injection..mysql_real_escape_string()if you are using the mysql functions, look at PDO prepared statements if you're using PDO. Also, if you're just expecting one of the variables to be integers, you can just typecast them to integer with$variable = (int) $variable;so that they can't be anything BUT an integer (which is recommended for integers, as it is much less processing).0if it isn't a numeric value.