Calculate Balance From Credit and Debit Using PHP MySQL


Hi friends, in this tutorial, you will learn how to calculate balance from credit and debit using PHP MySQL and get the direct code you can use to calculate running balances from the ledger. If you are wondering how to accomplish this task then you have come to the right place.

This is very important and often required in the case of accounting software or applications. However, if you are going to create any applications that need to display the running balance from the existing debit and credit that resides in the MySQL database then this tutorial will help you to accomplish this. On the other hand, you can get the balance according to individual users in the database if users exist.

Also read, Export Data From MySQL to Excel Using PHP

Steps to calculate balance from credit and debit using PHP MySQL

Step 1:- Establish the connection with the MySQL database as shown below.

dbconnect.php

<?php
$servername='localhost';
$username="root";
$password="";
try
{
	$con=new PDO("mysql:host=$servername;dbname=myproject_db",$username,$password);
	$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
	//echo 'connected';
}
catch(PDOException $e)
{
	echo '<br>'.$e->getMessage();
}
	
?>

Step 2:- Create a table in your MySQL database as shown below

DDL Information of the table

CREATE TABLE ledger (
id int(10) NOT NULL AUTO_INCREMENT,
transaction_date datetime NOT NULL,
credit varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
debit varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Step 3:- Insert credit and debit in the database with transaction dates as shown below.

Step 4:- Now you can display the data with running balance as shown below.

Calculate Balance From Credit and Debit Using PHP MySQL

Complete Code:-

<?php
  include('dbconnect.php');

  $sql = "SELECT id, transaction_date, credit, debit, COALESCE(((SELECT SUM(credit) FROM ledger b WHERE b.id <= a.id) - (SELECT SUM(debit) FROM ledger b WHERE b.id <= a.id)), 0) as balance FROM ledger a ORDER BY id DESC";
   $stmt = $con->prepare($sql);
   $stmt->execute();
   $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Calculate Balance From Credit and Debit Using PHP MySQL</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>

<div class="container mt-3" style="width:60%;">
  <h3>Calculate Balance From Credit and Debit Using PHP MySQL</h3>
  <br>   
  <table class="table table-bordered">
    <thead>
      <tr>
        <th>ID</th>
        <th>Credit</th>
        <th>Debit</th>
        <th>Balance</th>
        <th>Transaction Date</th>
      </tr>
    </thead>
    <tbody>
      <?php
        if(count($data)>0)
        {
          foreach($data as $key=>$value)
          {
          ?>
            <tr>
                    <td><?php echo $key+1; ?></td>
                    <td><?php echo $value['credit'];?></td>
                    <td><?php echo $value['debit'];?></td>
                    <td><?php echo $value['balance'];?></td>
                    <td><?php echo $value['transaction_date'];?></td>
                  </tr>
            <?php
          }
        }
      ?>  
      
    </tbody>
  </table>
</div>
</body>
</html>

Conclusion:- I hope this tutorial will surely resolve your problem. If there is any doubt then please leave a comment below.


Leave a Comment

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

sabung ayam online

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

KUPU178

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

RATU311

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

HOKI311

RATU311

THOR311

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

HOKI311 Link Apk Resmi

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311