【问题标题】:how to make a search in php mysql from multiple tables and display results in table如何在 php mysql 中从多个表中进行搜索并在表中显示结果
【发布时间】:2016-11-20 09:32:29
【问题描述】:

我是 php 新手,我想从多个表中进行搜索 它关于搜索。数据应以表格形式显示。我尝试了很多但无法解决问题。我看了很多教程,咨询了很多同事,但找不到任何可能的解决方案。请帮我。我会是你的慷慨行为。

我有 4 张桌子

(1)user
its have user_name,id 

(2)message
its have user_name (FK),messages
(3)

images
user_name(FK),images
(4)

videos
user_name,videos

我希望在搜索框中输入用户名,例如用户“rana”

它应该在表格中显示数据

rana 有这些图片、视频、消息

表格形式

这是我的表格

/*search.php*/

<form action="join.php" method="post" class="navbar-form navbar-right">
   <div class="input-group">
        <input type="Search" name="user_name" value="" placeholder="Search..." class="form-control" />
       <div class="input-group-btn">
           <button class="btn btn-info">

           <span class="glyphicon glyphicon-search"></span>

           </button>

       </div>

   </div>
   <a href="logout.php" class="btn btn-danger">logout <span class="glyphicon glyphicon-log-out"></span></a>
</form>

它是我的 php 搜索

 /*join.php*/


    <?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "terror_combat";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "select *from user a 
    join messages b on a.user_name = b.user_name
    join images c on a.user_name = c.user_name
    join videos d on a.user_name = d.user_name";

    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            echo "User Name: " . $row["user_name"]. "<br />";
            echo "Message: " . $row["message"]. "<br />";

            echo "Video  Path: " . $row["name"]. "<br />";
        echo'<img height="300" width="300" src="data:image;base64,'.$rows['image_path'].'">';
            //you get more data as your wise................
        }
    } else {
        echo "0 results";
    }

    mysqli_close($conn);
    ?>

我正在使用 php 和我的 sql 和 xamp

我希望在搜索框中输入用户名,例如用户“rana”

它应该在表格中显示数据

rana 有这些图片、视频、消息

以表格形式感谢您的帮助

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这就是您可以在表格中显示结果的方式。从 SQL 查询中获得结果后,打开表格并显示表格标题。

        echo "<table style='width:100%'>";
        echo "<tr>";
        echo "<th>Message</th>";
        echo "<th>Video</th>";
        echo "<th>Image</th>";
        echo "</tr>";
    

    现在循环遍历您的结果并显示表格行。

        if (mysqli_num_rows($result) > 0) {
            while($row = mysqli_fetch_assoc($result)) {
                echo "<tr>";
                echo "<td>" . $row['message']. "</td>";
                echo "<td>" . $row['videos'] . "</td>";
                echo "<td><img height='300' width='300' src='" . $rows['image_path'] . " />";
                echo "</tr>"
            }
        }
        else {
            echo "<tr>";
            echo "<td>No results</td>"
            echo "</tr>";
        }
    

    现在关闭你的桌子

        echo "</table>";
    

    【讨论】: