2016-11-20 88 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>";