Faseeh Haider
Faseeh Haider

Reputation: 11

how to make a search in php mysql from multiple tables and display results in table

i am new to php and i want to make search from multiple tables its about search. Data should be displayed in the table form. I tried alot but unable to resolve the issue. I watched many tutorials, counsulted many collegues but couldn't find any possible solution. Please help me. I would be a genourous act of you.

i have 4 tables

(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

i want that when i will write user name in search box like user "rana"

its should display data in table

rana has this images ,videos,messages

in a table form

this is my form

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

and its my php for search

 /*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);
    ?>

i am using php and my sql and xamp

i want that when i will write user name in search box like user "rana"

its should display data in table

rana has this images ,videos,messages

in a table form thanks for help

Upvotes: 1

Views: 706

Answers (1)

gwinyai
gwinyai

Reputation: 2580

This is how you could display your results in a table. Just after you get your results from your SQL query, open your table and display the table headings.

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

Now loop through your results and display the table rows.

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

Now close your table

    echo "</table>";

Upvotes: 1

Related Questions