<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "databasename";
$koneksi = mysqli_connect($host,$user,$password,$database);
if($koneksi->connect_error){
die("Koneksi gagal");
}
?>
2. Mencetak Data : print.php
<!DOCTYPE html>
<html>
<head>
<title>Print Data</title>
</head>
<body>
<center>
<h2>Laporan Data Barang </h4>
</center>
<?php
include 'koneksi.php';
?>
<table border="1" style="width: 100%">
<tr>
<th width="1%">No</th>
<th>Tanggal</th>
<th>Nama Barang</th>
<th width="5%">Jumlah</th>
</tr>
<?php
$no = 1;
$sql = mysqli_query($koneksi,"select * from barang_masuk");
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['tanggal']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['jumlah']; ?></td>
</tr>
<?php
}
?>
</table>
<script>
window.print();
</script>
</body>
</html>
3. Menampilkan data : index.php
<!DOCTYPE html>
<html>
<head>
<title>Print Data </title>
</head>
<body>
<center>
<h2>Print Data </a></h2>
<?php
include 'koneksi.php';
?>
<table border="1">
<tr>
<th>No</th>
<th>Tanggal</th>
<th>Nama Barang</th>
<th>Jumlah</th>
</tr>
<?php
$no = 1;
$sql = mysqli_query($koneksi,"select * from barang");
while($data = mysqli_fetch_array($sql)){
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['tanggal']; ?></td>
<td><?php echo $data['nama']; ?></td>
<td><?php echo $data['jumlah']; ?></td>
</tr>
<?php
}
?>
</table>
<br/>
<a href="print.php" target="_blank">CETAK</a>
</center>
</body>
</html>
Tampilan Hasil :
Kemudian klik CETAK