简体   繁体   中英

How to output all data from multiple tables using mysqli php

Most of the variables will be in my language sorry about that. Also keep in mind i have very little knowledge of php/mysql as even my schools teachers have no idea what is it.

In my database there are 4 tables. The tables only have primary keys. The tables do not have any foreign key or references to other tables. This is because in my school project we were told to not add foreign key and references. This means I cant/dont know how to use JOIN . Currently i am attempting to output all of the data from the 4 tables and output them as 1 using html/css.

My main problem is i do not know how to output data from different rows.

I have tried using mysqli_data_seek to select each row with my counter($no) but it seems that either im using it wrong or its just not supposed to do that. I have also tried using mysqli_fetch_assoc but i still get the same output.

I have a connection to my database

This is the code mysqli_data_seek

<?php

    $row = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
    $no = 0;

    if (mysqli_num_rows($row) > 0) {
    while ($stok = mysqli_fetch_assoc($row)) {

        $stok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
        $datastok = mysqli_data_seek($stok, $no);

        //get data from "barang' table
        $barang = mysqli_query($connection, "SELECT Nama_Barang, no_Barang from barang");
        $databarang = mysqli_data_seek($barang, $no);

        //get data from "lokasi"
        $lokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
        $datalokasi = mysqli_data_seek($lokasi, $no);

        //get data from "tarikh" table
        $tarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
        $datatarikh = mysqli_data_seek($tarikh, $no);

        $no++;

then after this i will have a output which is connected to a table already made (this is still in the while loop)

    echo '<tr>';
    echo '<td>'.$no.' </td>';
    echo '<td>'.$datastok.'</td>';
    echo '<td>'.$databarang.'</td>';
    echo '<td>'.$datalokasi.'</td>';
    echo '<td>'.$datastok.'</td>';
    echo '<tr>';

This is the code with mysqli_fetch_assoc

<?php

    $rowstok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
    $no = 0;

    if (mysqli_num_rows($rowstok)> 0) {
        while ($stok = mysqli_fetch_assoc($rowstok)) {


            $stok = mysqli_query($connection, "SELECT Kod_Barang, Kod_Rak, Harga, Tarikh_Jual from stok");
            $datastok = mysqli_fetch_assoc($stok);

            //get data from "barang" table
            $barang = mysqli_query($connection, "SELECT Nama_Barang, Bil_Barang from barang");
            $databarang = mysqli_fetch_assoc($barang);

            //get data from "lokasi" table
            $lokasi = mysqli_query($connection, "SELECT Jenis_Barang from lokasi");
            $datalokasi = mysqli_fetch_assoc($lokasi);

            //get data from "tarikh" table
            $tarikh = mysqli_query($connection, "SELECT Tarikh_Beli from tarikh");
            $datatarikh = mysqli_fetch_assoc($tarikh);

            $no++;

Again here is my output table

    //output table
    echo '<tr>';
    echo '<td>'.$no.' </td>';
    echo '<td>'.$datastok['Kod_Barang'].'</td>';
    echo '<td>'.$databarang['Nama_Barang'].'</td>';
    echo '<td>'.$datalokasi['Jenis_Barang'].'</td>';
    echo '<td>'.$datastok['Harga'].'</td>';
    echo '<tr>';

Right now the output is just showing the first's rows data multiple times based on number of row. And also my counter($no) in increasing by 2 (1,3,5,7,....) not as expected 1 I am trying to get it to be each rows data.

I got the answer thanks to @ADyson. I just changed my long line of code to JOIN

    $data = mysqli_query($connection , "SELECT *
FROM stok
INNER JOIN barang
      ON stok.Kod_Barang=barang.Kod_Barang
INNER JOIN lokasi
      ON stok.Kod_Rak=lokasi.Kod_Rak
INNER JOIN tarikh
      ON barang.Nama_Barang=tarikh.Nama_Barang");

again sorry for non English attributes.

here is where i learnt JOIN from another question SQL Inner join more than two tables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM