-2
<?php
include_once("config.php");
 
$result = mysqli_query($mysqli, "SELECT * FROM tb_buku JOIN tb_genre ON tb_buku.id_genre = tb_genre.id_genre JOIN tb_pengarang ON tb_buku.id_pengarang = tb_pengarang.id_genre");
?>
 
<html>
<head>    
    <title>Homepage</title>
</head>
 
<body>
<a href="add.php">Add New User</a><br/><br/>
 
    <table width='80%' border=1>
 
    <tr>
        <th>ID</th> <th>Judul</th> <th>Genre</th> <th>Tahun Terbit</th> <th>Pengarang</th> <th>Update</th>
    </tr>
    <?php  
    $no = 1;
    while($user_data = mysqli_fetch_array($result)) {   
             
        echo "<tr>";
        echo "<td>".$no++."</td>";      
        echo "<td>".$user_data['judul_buku']."</td>";
        echo "<td>".$user_data['nama_genre']."</td>";
        echo "<td>".$user_data['tahun_terbit']."</td>"; 
        echo "<td>".$user_data['nama_pengarang']."</td>";
        echo "<td><a href='edit.php?id_buku=$user_data[id_buku]'>Edit</a> | <a href='delete.php?id_buku=$user_data[id_buku]'>Delete</a></td></tr>";        
    }
    ?>
    </table>
</body>
</html>

Im currently trying to combine 2 tables into 1 tables to sync the id to all the database. How do i combine tb_genre and tb_pengarang into tb_buku? when i do it this way it gave me an error saying Uncaught TypeError: mysqli_fetch_array(): Argument #1 ($result) must be of type mysqli_result,

christan24
  • 29
  • 1
  • 1
  • 4
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – El_Vanja Apr 15 '21 at 15:05

1 Answers1

0

First of all, the error you are getting is because of your query. Your query is a boolean return one, which I guess it's returning false due to lack of error catching. I mean you need to check whether $result has something in it:

SELECT * FROM tb_buku JOIN tb_genre ON tb_buku.id_genre = tb_genre.id_genre JOIN tb_pengarang ON tb_buku.id_pengarang = tb_pengarang.id_genre

I really didn't understand clearly your main goal, which I'll assume that is joining two tables (tb_genre and tb_pengarang [in Indonesian, does it mean "author"?]) individually with tb_buku [I guess, it means "book"?] by their genre. And also it would be more comprehensible, if you'd shared your whole table structure. But no worries! I'll help as much as I can :)

So you have two solutions. One is dividing this whole operation into two queries which will be more readable and debuggable. Two is stuffing them into one statement with first merging tb_genre and tb_pengarang.

Assuming that your tables are like those:

tb_buku

id_buku judul_buku tahun_terbit buku_id_genre buku_id_pengarang
1 The Raven 1845 1 1
2 Ulysses 1904 2 2
3 The Lord of The Rings 1954 3 3
4 To Kill a Mockingbird 1960 4 4
5 The Last Day of a Condemned Man 1829 2 5

tb_genre

id_genre nama_genre
1 Horror
2 Drama
3 Fiction
4 Bildungsroman

tb_pengarang

id_pengarang nama_pengarang
1 Edgar Allan Poe
2 James Joyce
3 J. R. R. Tolkien
4 Harper Lee
5 Victor Hugo

You need to use this query, which I got help from this thread:

SELECT judul_buku, nama_genre, tahun_terbit, nama_pengarang, id_buku FROM tb_buku b JOIN tb_genre g on b.id_genre = g.id_genre JOIN tb_pengarang p on b.id_pengarang = p.id_pengarang

Which in code will look like this:

(...)
         
        <table width='80%' border=1>
     
        <tr>
            <th>ID</th> <th>Judul</th> <th>Genre</th> <th>Tahun Terbit</th> <th>Pengarang</th> <th>Update</th>
        </tr>
        
        <?php
        
        $no = 1; # You don't need to store this in a variable, as there is id_buku.
        $sql = "SELECT judul_buku, nama_genre, tahun_terbit, nama_pengarang, id_buku FROM tb_buku b JOIN tb_genre g on b.id_genre = g.id_genre JOIN tb_pengarang p on b.id_pengarang = p.id_pengarang";
    
        if ($result = mysqli_query($mysqli, $sql) && mysqli_num_rows($result) > 0)
        {
    
            while ($user_data = mysqli_fetch_array($result))
            {
            
                echo "<tr>";
                echo "<td>" . $no++ . "</td>";
                echo "<td>" . $user_data['judul_buku'] . "</td>";
                echo "<td>" . $user_data['nama_genre'] . "</td>";
                echo "<td>" . $user_data['tahun_terbit'] . "</td>";
                echo "<td>" . $user_data['nama_pengarang'] . "</td>";
                echo "<td><a href='edit.php?id_buku=$user_data[id_buku]'>Edit</a> | <a href='delete.php?id_buku=$user_data[id_buku]'>Delete</a></td></tr>";
            
            }
        }   
        ?>
        </table>

(...)

By the way, you don't need to use $no variable, as you get your id from the database. Also, it's more readable when you separate your SQL query from the function parameter.

I hope it resolves your problem. But in any case, I want to give my personal opinion. To improve the performance of your code, I mean, avoiding of the JOIN function every time when making a listing request like this, you can assemble all of your data under a common table, which in this case will be tb_daftar_buku (or tb_bibliography) where it'll be all of the record. When you make an operation on any table, you can update the main table and puf! all will get updated at once.

If you need help of this one too, I'll be here :) And also I hope that I made clearer this problem in your mind!

Roni
  • 91
  • 4