I am coding a reoccurring sql select with a varying clause. In the clause i have a PHP variable (supposed to be replaced later within a WHILE.
My code:
$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 works becuase its already defined.
$interest_id does NOT work because its not defined. I am trying to substitute it with $interest_id later on during a while loop in which I am calling $custaccount_id_clause as part of a while loop:
$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());
Within the $custaccout_id clause, I have the text '$interest_id' which I want PHP to replace the variable within. It is not replacing the variable.
This is a quick example app I am writing for a demo, please dont lecture me about which API I am using, as I wont be coding anything that goes into production! :)
sprintf()
is you friend. – Carsten Apr 8 at 8:48