在PHP中使用sprintf的MySQL查询

时间:2014-03-03 20:24:11

标签: php mysql

我有一个现有的查询,它使用sprintf将php和mysql合并到自定义的MySQL查询中。

这很有效:

$query_EmpVSP = sprintf("SELECT * FROM HTG_VSP WHERE HomeOfEmpNumber IS NULL OR HomeOfEmpNumber = '' OR HomeOfEmpNumber = %s", GetSQLValueString($colname_EmpVSP, "text"));

我需要为另一个名为CustomForEmpNumber的列添加第二个OR语句,并希望重用$ colname_EmpVSP值。

当我添加

. "OR HomeOfEmpNumber = %s", GetSQLValueString($colname_EmpVSP, "text"));

我在%s

获得了无效的语法

以下是显示错误的当前查询:

$query_EmpVSP = sprintf("SELECT * FROM HTG_VSP WHERE HomeOfEmpNumber IS NULL OR HomeOfEmpNumber = '' OR HomeOfEmpNumber = %s", GetSQLValueString($colname_EmpVSP, "text") . " OR CustomForEmpNumber = %s", GetSQLValueString($colname_EmpVSP, "text"));

1 个答案:

答案 0 :(得分:2)

试试这个:

$sql = "SELECT * FROM HTG_VSP WHERE HomeOfEmpNumber IS NULL OR HomeOfEmpNumber = '' OR HomeOfEmpNumber = %s or CustomForEmpNumber = %s"; 
$query_EmpVSP = sprintf($sql,GetSQLValueString($colname_EmpVSP, "text"),GetSQLValueString($colname_EmpVSP, "text"));