我正在用可变子句编写一个重新出现的sql select。在子句中,我有一个PHP变量(应该稍后在一段时间内替换)。
我的代码:
$custaccount_id_clause = "(SELECT DISTINCT(custaccount_id) AS custaccount_id FROM accountcomms_def_tbl a WHERE subagent_def_id = " . '\'$interest_id\'' . " AND agentorder > (SELECT agentorder FROM accountcomms_def_tbl WHERE subagent_def_id = '$subagent_def_id' and custaccount_id = a.custaccount_id))";$subagent_def_id的工作是因为它已经定义好了。
$interest_id不能工作,因为它没有定义。稍后,我试图在with循环期间用$interest_id替换它,在这个循环中,我调用$custaccount_id_clause作为with循环的一部分:
$allresidual_sql = mysql_query("SELECT SUM(txn_value) as txn_sum, COUNT(DISTINCT(custaccount_id)) AS accounts FROM account_stmnt_tbl WHERE txn_type IN ('Fixed Residual','Variable Residual','One Time Adjustment') AND custaccount_id IN ". $custaccount_id_clause)or die(mysql_error());发布于 2013-04-08 09:58:06
使用您想要使用的第一个变量($interest_id)的一个简单方法是测试它,并在其中放置一个null,而不是以如下方式直接使用该变量:
WHERE subagent_def_id = '" . ($interest_id ? $interest_id : null) . "' AND agentorder这应该可以防止您获得有关未定义的变量/属性的错误。
替代部分是吸引我的原因..。您的意思是,在您的结构中的给定点上,用$my_other_variable代替$my_other_variable吗?如果将整个SQL查询建模为一个字符串,并且根据给定的条件重写它,就可以实现这一点。
像这样的东西可能会起作用(这只是一个简单的测试,如果你觉得合适的话,可以根据你的需要调整它):
$query = ($a ? "SELECT * FROM `products` WHERE `price` > '{$a}';" : "SELECT * FROM `products` WHERE `date_added` < '{$b}' AND price > 20;");如果我没听错你的话。否则,我怀疑您希望使用eval()函数来解析编码的字符串,并使process成为您想要的变量。
希望这有帮助
https://stackoverflow.com/questions/15874528
复制相似问题