Postingan lainnya
Database error mysqli
gan ini kenapa ya?? udah coba otak atik masih aja sama ada mohon solusinya
ini koding functionnya gan
<?php
// koneksi ke database
// parameter 1.host 2.username 3.password 4.nama database
$dbtester = mysqli_connect("localhost","root","","dbtester");
function query($query){
global $dbtester;
$result = mysqli_query($dbtester, $query);
$rows = [];
while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
}
return $rows;
}
?>
ini coding indexnya
<?php
//memanggil function
require 'functions.php';
$product = query('SELECT * FROM product');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Halaman Admin</title>
<style>
img{
width: 60px;
height: 60px;
}
</style>
</head>
<body>
<h1>Daftar Product</h1>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>No.</th>
<th>Aksi</th>
<th>Gambar</th>
<th>Nama</th>
<th>Merk</th>
<th>Warna</th>
<th>Ukuran</th>
</tr>
<?php $i = 1; ?>
<?php foreach($product as $row) : ?>
<tr>
<td><?= $i;?></td>
<td>
<a href="#">Ubah</a> |
<a href="#">Hapus</a>
</td>
<td><img src="img/<?= $row ["gambar"]; ?>"></td>
<td><?= $row["nama"];?></td>
<td><?= $row["merk"];?></td>
<td><?= $row["warna"];?></td>
<td><?= $row["ukuran"];?></td>
</tr>
<?php $i++; ?>
<?php endforeach;?>
</table>
</body>
</html>
2 Jawaban:
<pre> function query($query) { global $dbtester; $sql = mysqli_query($dbtester, $query);
$result = array();
while($data = mysqli_fetch_assoc($sql)) {
$result[]=$data;
json_encode($data);
}
return $result;
} </pre>
<pre> <?php //memanggil function require 'functions.php'; $product = query('SELECT * FROM product'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Halaman Admin</title> <style> img{ width: 60px; height: 60px; } </style> </head> <body>
<h1>Daftar Product</h1>
<table border="1" cellpadding="10" cellspacing="0">
&lt;tr&gt;
&lt;th&gt;No.&lt;/th&gt;
&lt;th&gt;Aksi&lt;/th&gt;
&lt;th&gt;Gambar&lt;/th&gt;
&lt;th&gt;Nama&lt;/th&gt;
&lt;th&gt;Merk&lt;/th&gt;
&lt;th&gt;Warna&lt;/th&gt;
&lt;th&gt;Ukuran&lt;/th&gt;
&lt;/tr&gt;
&lt;?php $i = 1; ?&gt;
&lt;?php foreach($product as $row) : ?&gt;
&lt;tr&gt;
&lt;td&gt;&lt;?= $i;?&gt;&lt;/td&gt;
&lt;td&gt;
&lt;a href="#"&gt;Ubah&lt;/a&gt; |
&lt;a href="#"&gt;Hapus&lt;/a&gt;
&lt;/td&gt;
&lt;td&gt;&lt;img src="img/&lt;?= $row ["gambar"]; ?&gt;"&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $row["nama"];?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $row["merk"];?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $row["warna"];?&gt;&lt;/td&gt;
&lt;td&gt;&lt;?= $row["ukuran"];?&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;?php $i++; ?&gt;
&lt;?php endforeach;?&gt;
</table>
</body> </html> </pre>