[mysql]将多个表的结果合并到一个表在 php 中

标签: MySQL PHP HTML
发布时间: 2017/1/9 22:18:06
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

关于如何构建一个基本的搜索引擎,从我的问题是,显示的结果的时候它应该显示在它自己的表,在我的网页上的每个结果的数据库跟踪 web 教程我吗?我想要合并的结果,所以他们都在 html 中显示下一个表。

<?php
include("dbconnect.php");

if(!isset($_POST['search'])) {
header("Location:www.bacons.me");
}
$search_sql="SELECT * FROM users WHERE username OR FirstName LIKE '%".$_POST['search']."%'";
$search_query=mysql_query($search_sql);
if(mysql_num_rows($search_query) !=0) {
$search_rs=mysql_fetch_assoc($search_query);
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Bacons.me">
<meta name="keywords" content="HTML,CSS,JavaScript">
<meta name="author" content="James Narey">
<meta name=viewport content="width=device-width, initial-scale=1">

<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />


<link rel="stylesheet" type="text/css" href="main.css" >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<title>Bacons.me</title>
</head>
<body>
    <h1 class="logo"><a href="http://www.bacons.me/" class="logo-    a">BACONS.ME</a></h1>
    <nav>
        <ul>
            <li><a href="http://www.bacons.me" class="nav-a">Home</a></li>
            <li><a href="#" class="nav-a">About</a></li>
            <li><a href="search" class="nav-a">Search</a></li>
            <li><a href="contact" class="nav-a">Contact</a></li>
        </ul>
        <div class="handle">Menu<span class="arrow"> ▾</span></div>
    </nav>

<p>Search Results</p>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>
<table>
<tr>
    <th>Username</th>
    <th>First Name</th>
    <th>Surname</th>
    <th>Shift</th>
    <th>Agency</th>
</tr>
<tr>
        <td><?php echo $search_rs['username']; ?></td>
        <td><?php echo $search_rs['FirstName']; ?></td>
        <td><?php echo $search_rs['LastName']; ?></td>
        <td><?php echo $search_rs['Shift']; ?></td>
        <td><?php echo $search_rs['Agency']; ?></td>
</tr>
<br>
</table>
<?php
} while ($search_rs=mysql_fetch_assoc($search_query));

} else {
    echo "No results found";
    }


 ?>

 <html>
 <title>results</title>
 <head>
 </head>
 <body>
 </body>
 </html>
      <footer>
    <p class="footer">Website created by James Narey 2015.</p>
  </footer>

}

解决方法 1:

...} while ($search_rs=mysql_fetch_assoc($search_query));

我怀疑这就是问题。您每一次你取下一条记录创建一个表。你可能想要做这样的事情︰

<table>
<?php if(mysql_num_rows($search_query) !=0) {
do { ?>

// fill in table...

<?php
} while ($search_rs=mysql_fetch_assoc($search_query)); 
?>
</table>

注意在循环外表标记,所以你只会创建一个新的行,为你取的每条记录

赞助商