我是 php 的新手,我想从多个表中进行搜索 关于搜索。数据应以表格形式显示。我尝试了很多但无法解决问题。我看了很多教程,咨询了很多同事,但找不到任何可能的解决方案。请帮我。我会是你的慷慨行为。
我有4张 table
(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 有这些图片、视频、消息
以表格形式感谢您的帮助
最佳答案
这就是您在表格中显示结果的方式。从 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>";
}
现在关闭你的 table
echo "</table>";
关于php - 如何从多个表在php mysql中进行搜索并在表中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40702509/