我有下面的SQL查询返回包含的贷款,对具体日期做出金融交易结果...获取数据
$data = array('loan_id'=>130);
$STH = $DBH->prepare("SELECT * FROM ledger WHERE loan_id = :loan_id");
$STH->execute($data);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetchAll();
print_r($row);
此给出以下结果
Array (
[0] => Array (
[ledger_id] => 38
[loan_id] => 130
[ledger_type_id] => 1
[amount] => 1.20
[ledger_date] => 2016-07-25
)
[1] => Array (
[ledger_id] => 39
[loan_id] => 130
[ledger_type_id] => 3
[amount] => 0.90
[ledger_date] => 2016-08-15
)
[2] => Array (
[ledger_id] => 40
[loan_id] => 130
[ledger_type_id] => 2
[amount] => 0.30
[ledger_date] => 2016-09-19
)
)
我现在需要遍历贷款一直处于活动状态的所有日期,并查找循环中当前日期发生的事务的'ledger_type_id'和'金额'。分类账表中不包含每天的记录,但是我需要查看每个日期的贷款已被激活用于与此问题无关的其他目的。
$start_date = new DateTime('2016-07-13');
$today = new DateTime();
$today = $today->modify('+1 day'); // add one day to include today
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($start_date, $interval ,$today);
foreach ($daterange as $date) {
/*
The above SQL query returns a row with the 'ledger_date'
of '2016-08-15'
I need to find the value of the ledger_type_id
for the same row from the above query
when $date = '2016-08-15' in this loop
*/
// Do other unrelated stuff on each day of the loop.....
}
我尝试了一些使用in_array()但没有成功的东西。
但为什么这一切的并发症。为什么不用juse看看ledger_date? – e4c5
为什么不直接使用'AND ledger_date BETWEEN 2016-07-13 AND whatever' ???或'WHERE ledger_date ='2016-08-15'' – AbraCadaver
因为我需要循环每一个日期出于其他原因,并且分类账表每天都没有记录。顺便说一下,我已经编辑了这个问题来更清楚地解释这个问题 – ratherBeKiting