2016-11-20 84 views
1

我是新来的php,我想从多个表搜索 其关于搜索。数据应以表格形式显示。我尝试了很多,但无法解决问题。我看了很多教程,与许多同事见面,但找不到任何可能的解决方案。请帮帮我。我会成为你的一个慷慨行为。如何在多个表中搜索php mysql并在表中显示结果

我有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 

我想,当我将在搜索框中填写用户名,如用户“蛙”

它应该显示在表中的数据

蛙有这个图片,视频,消息

以表格的形式

这是我的形式

/*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> 

和我的搜索

/*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 PHP和我的SQL和XAMP

我想,当我会写在搜索框中的用户名like用户“rana”

它应该在表中显示数据

rana has this ima GES,视频,消息

在感谢表格形式

求助

回答

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>";