简体   繁体   中英

How to echo multiple table data one after the other in php/html

I have two tables Like:

Now i want to display first 5 from first table after that first 5 from send table and again next 5-10 from first table and next 5-10 from second table on so on.

I have code in this fashion aso please suggest me:-

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "songsind";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT sno, title, img, mp3 FROM ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo "<table style='width:100%'>
  <tr>
    <th>S.No</th>
    <th>Title of Song</th> 
    <th>Image</th>
    <th>MP3</th>
  </tr>";
    while($row = $result->fetch_assoc()) {

        echo "<tr><td> " . $row["sno"]. " </td><td> " . $row["title"]. "</td> <td><img src=' " . $row["img"]. " 'height='100' width='100'></img></td><td> <iframe src=' " . $row["mp3"]. " ' height = '100' width ='200' </iframe></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

You must select the table to fetch data from my sql

SELECT sno, title, img, mp3 FROM ";

Notice this statment

Your statement not full and you must use this:

SELECT sno, title, img, mp3 FROM (table name)";

I haven't tried, but this might work.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "songsind";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$tables = array("eros", "saregama", "sony", "tips");

foreach($tables as $tbl){
    $sql = "SELECT sno, title, img, mp3 FROM $tbl limit 5";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        echo "<table style='width:100%'>
      <tr>
        <th>S.No</th>
        <th>Title of Song</th> 
        <th>Image</th>
        <th>MP3</th>
      </tr>";
        while($row = $result->fetch_assoc()) {

            echo "<tr><td> " . $row["sno"]. " </td><td> " . $row["title"]. "</td> <td><img src=' " . $row["img"]. " 'height='100' width='100'></img></td><td> <iframe src=' " . $row["mp3"]. " ' height = '100' width ='200' </iframe></tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }
}
$conn->close();
?>

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