Php MySQL显示结果
请帮帮我。这里有人能告诉我为什么我数据库的结果会显示这样的链接吗Php MySQL显示结果,php,mysql,Php,Mysql,请帮帮我。这里有人能告诉我为什么我数据库的结果会显示这样的链接吗 Art Art Art Art Music Music Music Music Drama Drama Drama Drama Medical Medical Medical 我想让分类像这样显示 Art Music Drama Medical …这是我在index.php中的代码 $res = "SELECT * FROM table1 ORDER BY category ASC"; 下面是我
Art
Art
Art
Art
Music
Music
Music
Music
Drama
Drama
Drama
Drama
Medical
Medical
Medical
我想让分类像这样显示
Art
Music
Drama
Medical
…这是我在index.php中的代码
$res = "SELECT * FROM table1 ORDER BY category ASC";
下面是我为我的藏书创建的数据库
id |类别|作者|头衔
试试这个
在index.php中更改下面的行
$res = "SELECT * FROM table1 ORDER BY category ASC";
到
好的,我已经创建了一个简单的小例子,它应该给你一个如何解决这个问题的想法。这绝对不是一个最佳实践示例,我也没有花太多时间,但它确实起到了作用 这是为了将重点放在功能上,而不是性能上,只是向您展示一种方式,而不是一种方式 MySQL中的我的表: 书 类别 类别\它们之间的图书链接表 然后我有三个.php文件,称为category-overview.phpshows all categories、book-overview.phpshows特定类别内的所有书籍和book-view.phpshows关于一本特定书籍的信息 这些文件的代码: category-overview.php book-overview.php book-view.php 工作原理: 在category overview中,我所做的就是检索类别并为它们创建超链接 在book overview中,我在$\u GET变量中检查类别。如果有,请使用链接表检索具有该类别id的所有书籍。我通过检索所有书籍信息获得的ID创建超链接 在图书视图中,您只需执行相同的操作,但对于一个特定的图书检查$\u GET,如果找到,则检索该特定图书的数据
我希望它能澄清一下它是如何工作的。如果您有任何问题,请告诉我。是否希望将它们分组为:不查看重复项?是的,我试过了,但它只显示一个“作者”和“标题”,而不是“作者1-4”标题1-4。我似乎不明白你的问题。你到底想要实现什么?返回数据库中的数据,例如Category/Author/Title?index.php显示数据库中的所有“categories”。i、 e;'“艺术”显示4次,“音乐”显示4次,以此类推……您不想让它们显示4次吗?您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解在第1行的“=注意:未定义索引:C:\xampp\htdocs\books中的page_id”附近使用的正确语法您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解在第1yaa行“按类别分组”附近使用的正确语法。。。更改订单依据和分组依据的顺序。。。见编辑后的回答非常感谢。我会做的,让你知道。
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
$res = "SELECT * FROM table1 ORDER BY category ASC";
$res = "SELECT * FROM table1 GROUP BY category ORDER BY category ASC";
ID Name Author Description
1 Sample Book 1 Sample Author 1 Small description for book 1
2 Sample Book 2 Sample Author 2 Larger description for book 2 (BECAUSE WE EXTEND THIS BY SOME UNKONWN BLAH)
3 Sample Book 3 Sample Author 3 No description found for 3? Wrong! This is the description!
4 Sample Book 4 Sample Author 4 Description for book 4
5 Sample Book 5 Sample Author 5 Description for book 5
6 Sample Book 6 Sample Author 6 Description for book 6
ID name
1 art
2 music
3 drama
4 medical
ID category_id book_id
1 1 1
2 2 2
3 2 3
4 3 4
5 4 5
6 4 6
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM category ORDER BY name ASC";
$execute = mysql_query($query) or die(mysql_error());
$categories = array();
while ($row = mysql_fetch_assoc($execute)) {
$categories[] = $row;
}
foreach ($categories as $category) {
?>
<p><a href="book_overview.php?category=<?php echo $category['name'] ?>"><?php echo ucfirst($category['name']) ?></a> </p>
<?php } ?>
</body>
</html>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_GET['category'])) {
$category = $_GET['category'];
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM book b WHERE b.id IN (SELECT book_id FROM category_book WHERE category_id = (SELECT id FROM category WHERE name = '$category'));";
$execute = mysql_query($query) or die(mysql_error());
$books = array();
while ($row = mysql_fetch_assoc($execute)) {
$books[] = $row;
}
foreach ($books as $book) {
echo '<b>Book Title:</b> ' . $book['title'] . '<br/>';
echo '<b>Book Written By:</b> ' . $book['author'] . '<br/>';
echo '<a href="book_view.php?id=' . $book['id'] . '">Read more about this book...</a><br/>';
echo '<br/>';
}
}
?>
</body>
</html>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$dbh = mysql_connect("host", "username", "password") or die("Can't connect.");
mysql_select_db("books");
$query = "SELECT * FROM book b WHERE b.id = '$id';";
$execute = mysql_query($query) or die(mysql_error());
$book;
while ($row = mysql_fetch_assoc($execute)) {
$book = $row;
}
if ($book !== null) {
echo '<b>Book Title:</b> ' . $book['title'] . '<br/>';
echo '<b>Book Written By:</b> ' . $book['author'] . '<br/>';
echo '<p>' . $book['description'] . '</p><br/>';
echo '<br/>';
}
}
?>
</body>
</html>