rfresh
02-10-2010, 10:49 PM
I'm using sprintf() to formulate my sql statements, to prevent sql injections.
The problem I'm having is that some of my variables I use to construct my queries sometimes have data and sometimes not. They are filters and if there are no filters in effect, they are empty. But in the query single quotes display and it stops the query from working:
$php_SQL = sprintf("SELECT * FROM Employees
WHERE '%s' '%s' '%s'
AND company_index = '%d' ORDER BY last_name",
mysql_real_escape_string($name_filter),
mysql_real_escape_string($cell_phone_filter),
mysql_real_escape_string($dept_filter),
mysql_real_escape_string($_SESSION["php_g_company_index"]));
$name_filter may be empty but the '' shows up in the query and stops the query from working. I take out the '' and it seems to work ok that way but I'm not sure that is correct MySQL query syntax to do that?
$php_SQL = sprintf("SELECT * FROM Employees
WHERE %s %s %s
AND company_index = %d ORDER BY last_name",
mysql_real_escape_string($name_filter),
mysql_real_escape_string($cell_phone_filter),
mysql_real_escape_string($dept_filter),
mysql_real_escape_string($_SESSION["php_g_company_index"]));
Variables $cell_phone_filter and $dept_filter may or may not contain values.
Thanks for any help.
The problem I'm having is that some of my variables I use to construct my queries sometimes have data and sometimes not. They are filters and if there are no filters in effect, they are empty. But in the query single quotes display and it stops the query from working:
$php_SQL = sprintf("SELECT * FROM Employees
WHERE '%s' '%s' '%s'
AND company_index = '%d' ORDER BY last_name",
mysql_real_escape_string($name_filter),
mysql_real_escape_string($cell_phone_filter),
mysql_real_escape_string($dept_filter),
mysql_real_escape_string($_SESSION["php_g_company_index"]));
$name_filter may be empty but the '' shows up in the query and stops the query from working. I take out the '' and it seems to work ok that way but I'm not sure that is correct MySQL query syntax to do that?
$php_SQL = sprintf("SELECT * FROM Employees
WHERE %s %s %s
AND company_index = %d ORDER BY last_name",
mysql_real_escape_string($name_filter),
mysql_real_escape_string($cell_phone_filter),
mysql_real_escape_string($dept_filter),
mysql_real_escape_string($_SESSION["php_g_company_index"]));
Variables $cell_phone_filter and $dept_filter may or may not contain values.
Thanks for any help.