我已将我当前的代码(不多)放在此页面上供您参考(当然要减去真实密码): http: //mafgiftshop.org/MAF/queue/MP/for-reference.php
$delimiter = ',';
$db = new mysqli('localhost', 'mafgifts_mp', '******', 'mafgifts_mp');
if (($handle = fopen("mpdata.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
foreach($data as $i => $content) {
$data[$i] = $db->real_escape_string($content);
}
echo $data[$i]."";
$db->query("INSERT INTO `MP`
(GiftAmount,GoalAmount,GiftDate,FundID,FundDescription)
VALUES('" . implode("','", $data) . "');");
}
fclose($handle);
}
//THE ABOVE CODE PROPERLY PUTS ALL THE DATA INTO THE MySQL DATABASE, BUT I'M NOT
//ABLE TO DO THE CALCULATIONS I WANT AND INSERT ONLY THE RESULTING DATA.
所以我有一个包含 5 列信息的 CSV 文件。没有空白值。例如:
GiftAmount,GoalAmount,GiftDate,FundID,FundDescription(此行不在CSV中;仅供参考)
- "$10","$500","1/1/2012","8008","8008 基金说明"
- "$20","$500","2/10/2012","8008","8008 基金说明"
- "$62","$500","1/12/2012","8008","8008 基金说明"
- "$38","$1,000","3/31/2012","9102","9102 基金说明"
- "$21","$1,000","4/6/2012","9102","9102 基金说明"
我需要做的是在 FundID 相同时添加“GiftAmount”列,并将结果放入 MySQL 数据库。因此,对于上述数据,这将是生成的 MySQL 表:
- “92 美元” | “500 美元” | 《8008》 | “8008基金说明”(注意不再需要GiftDate)
- "$59" | “1,000 美元” | “9102” | 《9102基金说明》
我正在使用 PHP 来完成此操作,PHP 文件将每天在 Cron 作业中运行,因为 CSV 文件将根据不同基金的新礼物每天更改。
顺便说一句,CSV 文件目前有 8,794 行和很多很多 FundID。