目前,我正在调整站点的管理员端,允许站点管理员输入航班号,数据库将下拉航班上所有人员的姓名。但是,目前我的结果看起来像这样:

The following people are on this flight Joe BloggsThe following people are on this flight Gareth RobertsThe following people are on this flight Joe BloggsThe following people are on this flight Gareth RobertsThe following people are on this flight Joe BloggsThe following people are on this flight Gareth RobertsThe following people are on this flight Joe BloggsThe following people are on this flight Gareth RobertsThe following people are on this flight Joe BloggsThe following people are on this flight Gareth RobertsThe following people are on this flight Joe Bloggs

你明白了...名称显然是出于DPA原因组成的:)

我怎样才能说出例如:

旅客的航班号2021是:


名一
名字二
名称三


这是我的代码,我完全欢迎任何建议!

    <?
     include("logon/include/session.php");
                $loggedinuser = $session->username;
    $passengerNo = $_POST['enteredPassNo'];
       $theflightno = $_POST['enteredflightNo'];
            $query1 = mysql_query("SELECT p.surname, p.forename,   p.passNo FROM PASSENGER p WHERE p.passNo = '$passengerNo'");
while($row = mysql_fetch_array($query1))
{
    echo "Welcome to the site"  . " " . $row['forename'] ." ";
        }
        echo mysql_error();
$query2 = mysql_query("SELECT    p.surname, p.forename, p.passNo, p.flightNo FROM PASSENGER p, FLIGHT_INFO f WHERE     p.flightNo = '$theflightno'");
while($row = mysql_fetch_array($query2))
{
    echo "The following people are on this flight" . "</ br> ";
    echo $row['forename'] ." " . $row['surname'];
    echo "</ br>";
        echo mysql_error();
        }
    echo mysql_error();
       ?>


更加关注的是query2,确实将query1用于一些测试!
为了澄清起见,存储在$ passengerNo和$ theflightNo中的值是从表单中发布后正确分配的。

非常感谢,

汤姆

编辑:关于格式的很好的建议,但仍在这样打印:-

以下人员正在乘飞机


加雷斯·罗伯茨
乔·博格斯
加雷斯·罗伯茨
乔·博格斯
加雷斯·罗伯茨
乔·博格斯


而不是打印

以下人员正在飞行:-


加雷斯·罗伯茨
乔·博格斯


等一次:)

最佳答案

$query2之后的段更改为此:

$query2 = mysql_query("SELECT CONCAT(p.surname, p.forename) as name, p.passNo,
         p.flightNo FROM PASSENGER p WHERE p.flightNo = '$theflightno'
         GROUP BY name");
echo "Passengers on flight number {$theflightno} are:" . "<br /> <ul>";
while($row = mysql_fetch_array($query2)) {
  echo "<li>" . $row['name'] . "</li>";
  echo "</ br>";
}
echo "</ul>";


编辑

我所做的基本编辑是:


添加了CONCAT函数,以便将名称作为单个字符串返回(用于重复的姓氏/姓氏值)。
更改查询,以包含GROUP BY子句。这样重复的名称(forname + surname)将只出现一次。

关于php - 使用PHP和mySQL格式化搜索结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10248933/

10-09 19:24