简体   繁体   中英

Show results from mysql query with php 5

<table class="table table-bordered table-hover table-responsive bgwhite clgray">
          <thead>
            <tr>
              <th class="text-center text-capitalize" width="20">
                No
              </th>
              <th class="text-center text-capitalize">
                idbarang
              </th>
              <th class="text-center text-capitalize">
                Kategori
              </th>
              <th class="text-center text-capitalize">
                Merk
              </th>
              <th class="text-center text-capitalize">
                Foto
              </th>
              <th class="text-center text-capitalize">
                Stok
              </th>
            </tr>
          </thead>
          <?php
            $queryStok  = "SELECT SUM(stok) AS jumlahbarang FROM tabelstok WHERE idjenisusaha=1 GROUP BY idbarang";
            $resultStok = mysqli_query($link,$queryStok);
            $queryStok2 = "SELECT * FROM tabelbarang,tabelmerk WHERE tabelbarang.idmerk=tabelmerk.idmerk";
            $resultStok2= mysqli_query($link,$queryStok2);
            while ($datastok=mysqli_fetch_assoc($resultStok,$resultStok2)) {
          ?>
          <tbody>
            <tr>
              <td class="text-center text-capitalize">
                <?php echo $datastok['merk'] ?>
              </td>
              <td class="text-center text-capitalize">
                No
              </td>
              <td class="text-center text-capitalize">
                No
              </td>
              <td class="text-center text-capitalize">
                No
              </td>
              <td class="text-center text-capitalize">
                No
              </td>
              <td class="text-center text-capitalize">
                <?php echo $datastok['jumlahbarang'];?>
              </td>

          </tbody>
          <?php  }  ?>
        </table>

How can I show the result of 2 tables with 2 queries, in A SINGLE loop.

  1. If you want to show the result of two queries without writting two while loops, you should use mysqli_multi_query() function to execute multiple queries at once. Each statement should have a semicollon at the end (the last statement should not contain the semicollon). You can add as many queries as you want.

     $query = "YOUR FIRST STATEMENT;"; $query .= "YOUR SECOND STATEMENT"; $result = mysqli_multi_query($link, $query); do { $result = mysqli_store_result($link); while ($row = mysqli_fetch_row($result)) { // DO YOUR STUFF HERE WITH $row } mysqli_free_result($result); } while (mysqli_next_result($link)); 
  2. If you want to merge the queries into one, then if there is a column which connects/associates both tables, then you should use JOIN when you build the query. Example:

     SELECT customers.id, customers.name FROM customers INNER JOIN details ON customers.id=details.user_id; 

    This merges the elements from details table with elements from customers table, linked by a common value (the customers ids).

As you didn't show the structure of the database, and tables I can't show an example based on your code, just a general one.

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