In the past, before I switched over to Zend Framework, I ran all my PHP variables, to be used in a MySQL statement, through the following function. I'm not sure if it's actually 100% secure, but it seemed to be. I think Salathe, a while ago, pointed out some problems regarding different file-types, such as boolean values, but I don't believe anybody actually discovered any such major flaws.
It'll serve as a good base nonetheless, for if you wish to add onto it, or switch some things around, or merely use it as a reference.
I did use it in conjunction with
sprintf, as you mentioned. This function places in any quotes for you, and so you shouldn't actually put in any quotes into
sprintf's first argument.
To give an example:
php Code:
$szMyVar1 =
'TalkPHP.com';
$szMyVar2 =
'TalkRSI.com';
$szSQL =
sprintf( "SELECT * FROM myTable WHERE myColumn = %s OR myColumn = %s",
mysql_parse_value
($szMyVar1), mysql_parse_value
($szMyVar2));