Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
Fatal error: Uncaught Error: Call to a member function query() on null in C:\
Saya kan mau membuat data tables dari formulir yg sudah dibuat, pas saya membuat listnya terjadi error seperti ini
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\prakH\koneksi.php:18 Stack trace: #0 C:\xampp\htdocs\prakH\list_formulir.php(34): koneksi->select_formulir() #1 {main} thrown in C:\xampp\htdocs\prakH\koneksi.php on line 18
untuk kode koneksi.php nya ini
<?php
class koneksi
{
var $conn;
function __construct()
{
$servername = "localhost";
$username = "root";
$password = "";
$databasename = 'formulir';
$conn = mysqli_connect($servername, $username, $password, $databasename);
}
public function select_formulir()
{
$sql = "SELECT * FROM formulir";
return $this->conn->query($sql);
}
function insert_formulir()
{
$nama = $_POST["nama"];
$kelas = $_POST["kelas"];
$nim = $_POST["nim"];
$jenis_kelamin = $_POST["jenis_kelamin"];
$sql = "INSERT INTO data (nama, nim, kelas, jenis_kelamin) VALUES ('$nama', '$nim', '$kelas', '$jenis_kelamin')";
return $this->conn->query($sql);
}
}
$koneksi = new Koneksi();
if (isset($_GET["insert formulir"])) {
$koneksi->insert_formulir();
}
?>
untuk code list_formulir nya ini
<?php
include_once "koneksi.php";
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel=stylesheet" type="text/css" href="assets/css/bootstrap.css">
</head>
<body>
<body>
<div class="container-fluid mt-5">
<!- - Button Trigger modal - ->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#statisBackdrop">
Tambah Data
</button>
<table class="table table-hover">
<thead
<tr>
<th>Nama</th>
<th>NIM</th>
<th>Kelas</th>
<th>Jenis Kelamin</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php
$result = $koneksi->select_formulir();
$no = 1;
?>
<?php foreach ($result as $row): ?>
<tr>
<th><?= $no++?></th>
<th><?= $row["nama"] ?></th>
<th><?= $row["nim"] ?></th>
<th><?= $row["kelas"] ?></th>
<th><?= $row["jenis_kelamin"] ?></th>
<th><a href="" class="badge badge-success">Edit</a> <a href="" class="badge badge-danger">Hapus</a></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
<!- - Modal - ->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Tambah Data</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
. . .
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="assets/test/bootstrap"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
bagaimana ya cara baikinnya?
Tanggapan
itu ga ada fungsi querynya
1 Jawaban:
<div>Hallo izin bantu jawab yaa,<br>Kamu mengkoneksikan database mysql menggunakan function mysql_connect, dimana setau saya ini function biasa atau istilah prosedural, sehingga pada penggunaan baris kode ini :</div><pre><em>return</em> $this->conn->query($sql);</pre><div>itu menimbulkan error, karena function mysqli_connect itu bukan object.<br><br>Solusinya kamu bisa ubah baris kode koneksi kamu dari :</div><pre>$conn = mysqli_connect($servername, $username, $password, $databasename);</pre><div>menjadi seperti ini :</div><pre>$conn = new mysqli($servername, $username, $password, $databasename);</pre><div><br>Silahkan cek referensi berikut ini :<br>- <a href="https://www.php.net/manual/en/mysqli.construct.php">https://www.php.net/manual/en/mysqli.construct.php</a><br>- <a href="https://www.php.net/manual/en/function.mysqli-connect.php">https://www.php.net/manual/en/function.mysqli-connect.php</a><br><br>Semoga berhasil ya.<br><br></div>