-1

我的数据库表“resbook”中有以下数据

      htno             name             fname

       2              Rakesh             Rahul
       2              Rakesh             Rahul
       2              Rakesh             Rahul
       2              Rakesh             Rahul
       2              Rakesh             Rahul
       3              Himesh            Krishna
       3              Himesh            Krishna
       3              Himesh            Krishna
       3              Himesh            Krishna
       3              Himesh            Krishna

我有一个表单要求用户输入“htno”,并且我只想为相应的“htno”显示一次“名称”和“fname”,因为相同的行重复了五次

这是我的代码

<?PHP
$userInputEntities = htmlentities($userInput);
echo $userInputEntities;

$username = "admin";
$password = "123456";
$database = "resbook";
$server = "localhost";
$db = new PDO ("mysql:host=$server;dbname=$database", "$username", "$password");

if ($db) {
$id = $_GET['id'];
$NAME = $db->prepare("SELECT * FROM resbook WHERE htno=:id");
$NAME -> execute(array(':id'=>$id));
while ($row1 = $NAME->fetch(PDO::FETCH_ASSOC)){
echo "<td align=left><font size='2'> STUDENT NAME :</font><font color='blue' size='2'><b> " . $row1['name']. "</b></font></td>";
echo "<strong>&emsp;&emsp;||&emsp;&emsp;</strong>";
echo "<td align=left><font size='2'> FATHER NAME :</font><font color='blue' size='2'><b> " . $row1['fname']. "</b><p></font></td>";
}

使用此代码,我将输出重复 5 次,因为表中的每个 htno 有 5 行

STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul
STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul
STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul
STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul
STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul

我只想像这样输出一次

STUDENT NAME : Rakesh            ||        FATHER NAME : Rahul

请帮帮我

4

6 回答 6

1
SELECT distinct(htno),name,fname FROM resbook WHERE htno=:id
于 2013-06-06T06:31:42.670 回答
0

DISTINCT在查询中使用

$NAME = $db->prepare("SELECT DISTINCT(htno),* FROM resbook WHERE htno=:id");
于 2013-06-06T06:26:14.283 回答
0

改变

$NAME = $db->prepare("SELECT * FROM resbook WHERE htno=:id");

$NAME = $db->prepare("SELECT DISTINCT * FROM resbook WHERE htno=:id");

DISTINCT 关键字用于仅返回不同(不同)的值。

编辑:-

你也可以试试这个:-

SELECT htno,distinct(name),fname FROM resbook WHERE htno=:id
于 2013-06-06T06:28:41.187 回答
0

尝试限制 1:

SELECT * FROM resbook WHERE htno=:id LIMIT 1
于 2013-06-06T06:32:19.963 回答
0

DISTINCT *不削减它。您应该只运行DISTINCT您希望返回唯一的特定字段。

尝试

SELECT distinct(htno),name,fname FROM resbook WHERE htno=:id
于 2013-06-06T06:33:31.453 回答
0

用这种方式写查询.... SELECT * FROM resbook WHERE htno=id LIMIT 1

希望它会帮助你。

于 2013-06-06T13:06:52.450 回答