Friday, January 2, 2026

.::: Sample Access Database MariaDB via Frontend & Backend using php :::.

 

1. instal httpd

2. config 


[root@teguhth-all html]# pwd
/var/www/html
[root@teguhth-all html]# cat config.php
<?php
$host = "10.10.10.90";
$user = "admin";
$pass = "admin";
$db   = "teguhth";
$port = 3306;

$conn = new mysqli($host, $user, $pass, $db, $port);

if ($conn->connect_error) {
    die("Koneksi gagal: " . $conn->connect_error);
}
?>

[root@teguhth-all html]#
  

3. Backend

[root@teguhth-all html]# pwd
/var/www/html
[root@teguhth-all html]# cat pembelian.php
<?php
header("Content-Type: application/json");
require 'config.php';

$sql = "SELECT
            KODE_PEMBELIAN,
            KODE_BARANG,
            KODE_CUSTOMER,
            TANGGAL_PEMBELIAN,
            JUMLAH_PEMBELIAN
        FROM pembelian";

$result = $conn->query($sql);

$data = [];
while ($row = $result->fetch_assoc()) {
    $data[] = $row;
}

echo json_encode($data, JSON_PRETTY_PRINT);
$conn->close();
?>

[root@teguhth-all html]#
 

4. front end 

[root@teguhth-all html]# pwd
/var/www/html
[root@teguhth-all html]# cat index.html
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <title>Data Pembelian</title>
    <style>
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #aaa; padding: 8px; }
        th { background: #f2f2f2; }
    </style>
</head>
<body>

<h2>Data Pembelian</h2>

<table>
    <thead>
        <tr>
            <th>Kode Pembelian</th>
            <th>Kode Barang</th>
            <th>Kode Customer</th>
            <th>Tanggal</th>
            <th>Jumlah</th>
        </tr>
    </thead>
    <tbody id="data-pembelian"></tbody>
</table>

<script>
fetch("pembelian.php")
.then(response => response.json())
.then(data => {
    let tbody = document.getElementById("data-pembelian");
    data.forEach(row => {
        let tr = `
            <tr>
                <td>${row.KODE_PEMBELIAN}</td>
                <td>${row.KODE_BARANG}</td>
                <td>${row.KODE_CUSTOMER}</td>
                <td>${row.TANGGAL_PEMBELIAN}</td>
                <td>${row.JUMLAH_PEMBELIAN}</td>
            </tr>
        `;
        tbody.innerHTML += tr;
    });
});
</script>

</body>
</html>

[root@teguhth-all html]#
 

 

No comments:

Post a Comment

Popular Posts