mysqli_extension.php
<?php
$DBServer = 'localhost';
$DBUser = 'root';
$DBPass = '';
$DBName = 'db_webinarcruds';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
function getData($conn,$sql){
$rs = $conn->query($sql);
$arr=$rs->fetch_all(MYSQLI_ASSOC);
$rs->free_result();
return $arr;
}
?>
$DBServer = 'localhost';
$DBUser = 'root';
$DBPass = '';
$DBName = 'db_webinarcruds';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
function getData($conn,$sql){
$rs = $conn->query($sql);
$arr=$rs->fetch_all(MYSQLI_ASSOC);
$rs->free_result();
return $arr;
}
?>
latmysqli.php
<?php
require_once"mysqli_extension.php";
require_once"datatable.php";
?>
<h1>Koneksi Database Mysqli Extension</h1>
<table id="examplec" class="display" style="width:100%">
<thead>
<tr>
<th>IDKat</th>
<th>Nama Kategori</th>
<th>Deskripsi</th>
<th>Menu</th>
</tr>
</thead>
<tbody>
<?php
$sql="select * from `tb_kategori`";
$arr=getData($conn,$sql);
foreach($arr as $d) {
$id_kategori=$d["id_kategori"];
$nama_kategori=$d["nama_kategori"];
$deskripsi=$d["deskripsi"];
$deskripsi = wordwrap($deskripsi, 100, "<br />");
echo "<tr>
<td>$id_kategori</td>
<td>$nama_kategori</td>
<td><small>$deskripsi</small></td>
<td>
<button type='button' class='btn btn-primary'>Ubah</button>
|
<button type='button' class='btn btn-primary'>Hapus</button>
</td>
</tr> ";
}
?>
</tbody>
<tfoot>
<tr>
<th>IDKat</th>
<th>Nama Kategori</th>
<th>Deskripsi</th>
<th>Menu</th>
</tr>
</tfoot>
</table>
<?php
function getJum($conn,$sql){
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
function getField($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$d= $rs->fetch_assoc();
$rs->free();
return $d;
}
function process($conn,$sql){
$s=false;
$conn->autocommit(FALSE);
try {
$rs = $conn->query($sql);
if($rs){
$conn->commit();
$last_inserted_id = $conn->insert_id;
$affected_rows = $conn->affected_rows;
$s=true;
}
}
catch (Exception $e) {
echo 'fail: ' . $e->getMessage();
$conn->rollback();
}
$conn->autocommit(TRUE);
return $s;
}
function getKategori($conn,$kode){
$field="nama_kategori";
$sql="SELECT `$field` FROM `tb_kategori` where `id_kategori`='$kode'";
$rs=$conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}
?>
require_once"mysqli_extension.php";
require_once"datatable.php";
?>
<h1>Koneksi Database Mysqli Extension</h1>
<table id="examplec" class="display" style="width:100%">
<thead>
<tr>
<th>IDKat</th>
<th>Nama Kategori</th>
<th>Deskripsi</th>
<th>Menu</th>
</tr>
</thead>
<tbody>
<?php
$sql="select * from `tb_kategori`";
$arr=getData($conn,$sql);
foreach($arr as $d) {
$id_kategori=$d["id_kategori"];
$nama_kategori=$d["nama_kategori"];
$deskripsi=$d["deskripsi"];
$deskripsi = wordwrap($deskripsi, 100, "<br />");
echo "<tr>
<td>$id_kategori</td>
<td>$nama_kategori</td>
<td><small>$deskripsi</small></td>
<td>
<button type='button' class='btn btn-primary'>Ubah</button>
|
<button type='button' class='btn btn-primary'>Hapus</button>
</td>
</tr> ";
}
?>
</tbody>
<tfoot>
<tr>
<th>IDKat</th>
<th>Nama Kategori</th>
<th>Deskripsi</th>
<th>Menu</th>
</tr>
</tfoot>
</table>
<?php
function getJum($conn,$sql){
$rs=$conn->query($sql);
$jum= $rs->num_rows;
$rs->free();
return $jum;
}
function getField($conn,$sql){
$rs=$conn->query($sql);
$rs->data_seek(0);
$d= $rs->fetch_assoc();
$rs->free();
return $d;
}
function process($conn,$sql){
$s=false;
$conn->autocommit(FALSE);
try {
$rs = $conn->query($sql);
if($rs){
$conn->commit();
$last_inserted_id = $conn->insert_id;
$affected_rows = $conn->affected_rows;
$s=true;
}
}
catch (Exception $e) {
echo 'fail: ' . $e->getMessage();
$conn->rollback();
}
$conn->autocommit(TRUE);
return $s;
}
function getKategori($conn,$kode){
$field="nama_kategori";
$sql="SELECT `$field` FROM `tb_kategori` where `id_kategori`='$kode'";
$rs=$conn->query($sql);
$rs->data_seek(0);
$row = $rs->fetch_assoc();
$rs->free();
return $row[$field];
}
?>
datatable.php
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#examplec').DataTable();
} );
</script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#examplec').DataTable();
} );
</script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css">
Tidak ada komentar:
Posting Komentar