setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true); // 이 스크립트의 메모리 제한 증가 ini_set('memory_limit', '256M'); $sql = "SELECT * FROM " . $DB . "." . $tablename; try { $stmh = $pdo->prepare($sql); $stmh->execute(); $rows = $stmh->fetchAll(PDO::FETCH_ASSOC); $pdo->beginTransaction(); foreach ($rows as $row) { // estimateList JSON 파싱 $estimateListData = json_decode($row['estimateList'], true); $decided_estimate = 0; $issued_amount = 0; if (is_array($estimateListData)) { foreach ($estimateListData as $estimate) { if (!empty($estimate['col4'])) { $decided_estimate = floatval(str_replace(',', '', $estimate['col4'])); $issued_amount += $decided_estimate; } } } // accountList JSON 파싱 $accountListData = json_decode($row['accountList'], true); $totalBill = 0; $totalDeposit = 0; if (is_array($accountListData)) { foreach ($accountListData as $account) { if (!empty($account['col4'])) { $totalBill += floatval(str_replace(',', '', $account['col4'])); } if (!empty($account['col6'])) { $totalDeposit += floatval(str_replace(',', '', $account['col6'])); } } } // 최종 계산 $issued_receivable = $totalBill - $totalDeposit; $total_receivable = $decided_estimate - $totalDeposit; // 데이터베이스 업데이트 $updateSql = "UPDATE $DB.$tablename SET decided_estimate = :decided_estimate, issued_amount = :issued_amount, issued_receivable = :issued_receivable, total_receivable = :total_receivable, total_deposit = :total_deposit WHERE num = :num"; $updateStmh = $pdo->prepare($updateSql); $updateStmh->bindValue(':decided_estimate', $decided_estimate, PDO::PARAM_STR); $updateStmh->bindValue(':issued_amount', $totalBill, PDO::PARAM_STR); $updateStmh->bindValue(':issued_receivable', $issued_receivable, PDO::PARAM_STR); $updateStmh->bindValue(':total_receivable', $total_receivable, PDO::PARAM_STR); $updateStmh->bindValue(':total_deposit', $totalDeposit, PDO::PARAM_STR); $updateStmh->bindValue(':num', $row['num'], PDO::PARAM_INT); $updateStmh->execute(); } $pdo->commit(); echo "모든 레코드가 성공적으로 업데이트되었습니다."; } catch (PDOException $Exception) { $pdo->rollBack(); echo "오류: " . $Exception->getMessage(); } ?>