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?
avatar karu

@karu

2 Kontribusi 0 Poin


Jawaban

Hallo izin bantu jawab yaa,
Kamu mengkoneksikan database mysql menggunakan function mysql_connect, dimana setau saya ini function biasa atau istilah prosedural, sehingga pada penggunaan baris kode ini :
return $this->conn->query($sql);
itu menimbulkan error, karena function mysqli_connect itu bukan object.

Solusinya kamu bisa ubah baris kode koneksi kamu dari :
$conn = mysqli_connect($servername, $username, $password, $databasename);
menjadi seperti ini :
$conn = new mysqli($servername, $username, $password, $databasename);

Silahkan cek referensi berikut ini :
- https://www.php.net/manual/en/mysqli.construct.php
- https://www.php.net/manual/en/function.mysqli-connect.php

Semoga berhasil ya.

avatar ahanafi

@ahanafi

745 Kontribusi 486 Poin


Login untuk gabung berdiskusi
premium logo tube

Hey, sedang ada diskon premium untuk akses semua kelas. Lihat di sini