gpt4 book ai didi

php - MySQL 显示结果

转载 作者:行者123 更新时间:2023-11-30 23:03:36 24 4
gpt4 key购买 nike

请帮帮我。这里的任何人都可以告诉我为什么我的数据库的结果显示这样的链接吗?

 Art
Art
Art
Art
Music
Music
Music
Music
Drama
Drama
Drama
Drama
Medical
Medical
Medical

我想要这样显示类别,

Art
Music
Drama
Medical

...这是我在 index.php

中的代码
<?php 

$dbh=mysql_connect("127.0.0.1","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("books");


$res = "SELECT * FROM table1 ORDER BY category ASC";
$res_query = mysql_query($res) or die (mysql_error());
$ra = mysql_fetch_assoc($res_query);

?>



<?php do { ?>
<p><a href="page.php?id=<?php echo $ra['id']; ?>"><?php echo $ra['category']; ?></a></p>
<?php } while ($ra = mysql_fetch_assoc($res_query))?>

...这是我在 page.php 中显示结果的代码...

<?php 

$dbh=mysql_connect("127.0.0.1","root","root") or die ('Cannot connedt to the Database' .mysql_errno());
mysql_select_db("books");

if(isset($_GET['id'])){
$id = $_GET['id'];
}else {
$id=1;
}
$res = "SELECT * FROM table1 WHERE id=$id";
$res_query = mysql_query($res) or die (mysql_error());
$ra = mysql_fetch_assoc($res_query);

?>


<h1><?php echo $ra['category']; ?></h1>
<p><?php echo $ra['author']; ?></p>
<p><?php echo $ra['title']; ?></p>

下面是我为我的藏书创建的数据库。


编号 |类别 |作者 |标题

1       |Art        |   Author1  |      title1
2 |Art | Author2 | title2
3 |Art | Author3 | title3
4 |Art | Author4 | title4
5 |Music | Author1 | title1
6 |Music | Author2 | title2
7 |Music | Author3 | title3
8 |Music | Author4 | title4
9 |Drama | Author1 | title1
10 |Drama | Author2 | title2
11 |Drama | Author3 | title3
12 |Drama | Author4 | title4
13 |Medical | Author1 | title1
14 |Medical | Author2 | title2
15 |Medical | Author3 | title3

最佳答案

试试这个,

修改 index.php 中的下面一行

$res = "SELECT * FROM table1 ORDER BY category ASC";

$res = "SELECT * FROM table1 GROUP BY category ORDER BY category ASC";

关于php - MySQL 显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22803849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com