【发布时间】:2012-03-08 00:02:48
【问题描述】:
我是编程新手,碰壁了。我正在尝试构建一个简单的链接站点作为学习 mysql 和 php 的起点。
我有两个表:类别 - 链接
在类别表中,我列出了 35 个类别。 id (int) 自增主键和类别 varchul
在链接中我有 id(int)auto increment 主键、链接名称、url 和描述。
我已经到了可以列出要列出的类别并使用 category?id=$id 为每个类别创建一个 url 的地步。但我无法获取与类别关联的链接以按类别显示。我要么得到所有链接,要么没有链接。我使用过 join、left join、right join,其中 links.categories=categories.categories 。没有任何效果。这是获取类别的代码,我只是不知道如何获取每只猫的链接。谁能指出我正确的方向。
enter code here
<? include "db.php" ?>
<?php
$id = intval($_GET['id']); // force user input to an int
if(!empty($id)) // if the user input is not empty or 0
{
$query = "SELECT * FROM categories WHERE id = $id LIMIT 1"; // attempt to select the row for the given id
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) > 0) // if the categorie was found
{
$row = mysql_fetch_assoc($result); // fetch the resultset to an array called $row
echo $row['categories']; // echo the categories field from the row
// etc.
}
else
{
die('Error: Bad ID'); // the categories was not found
}
}
else
{
die('Error: Bad ID'); // the id given was not valid
}
mysql_close();
?>
我希望有人可以提供帮助。提前致谢
【问题讨论】:
标签: mysql