首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在php/html中一个接一个地回显多个表数据

如何在php/html中一个接一个地回显多个表数据
EN

Stack Overflow用户
提问于 2017-09-11 21:42:16
回答 2查看 568关注 0票数 0

我有两个表,如:

现在我想要显示第一个表中的前5个,然后是发送表中的第一个5,然后是第一个表中的下一个5-10,第二个表中的下一个5-10,依此类推。

我有这种方式的代码,请建议我:-

代码语言:javascript
运行
复制
<?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();
?>
EN

Stack Overflow用户

发布于 2017-09-11 21:57:45

我还没试过,但这可能行得通。

代码语言:javascript
运行
复制
<?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();
?>
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46157191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档