我刚刚意识到自己的错误!我两次使用表jokecategory ...学习PHP是一种令人羞愧的经历......
感谢您的帮助,到目前为止一切顺利!
抱歉,我没有粘贴SQL,但你的建议仍有帮助。我修改了SQL查询以使用“AS”,但我认为我将某些内容与“类别”表混合在一起。无法弄清楚是什么,但它现在不允许我根据类别进行搜索。有任何想法吗?我收到以下错误:
错误:不唯一的表/别名:'jokecategory'
SQL查询:
$select = 'SELECT DISTINCT joke.id, joke.joketext, author.id AS author_name, author.name AS author_name, jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, category.id AS cat_id, category.name as cat_name';
$from = ' FROM joke, author, jokecategory, category';
$where = ' WHERE joke.authorid = author.id AND joke.id = jokecategory.jokeid AND jokecategory.categoryid = category.id';
$aid = $_POST['aid'];
if ($aid != '') { // An author is selected
$where .= " AND authorid='$aid'";
}
$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ', jokecategory';
$where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'";
}
$tid = $_POST['tid'];
if ($tid != '') { // A theme is selected
$from .= ', joketheme';
$where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'";
}
$gfid = $_POST['gfid'];
if ($gfid != '') { // A region is selected
$from .= ', jokegeofocus';
$where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'";
}
$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND joketext LIKE '%$searchtext%'";
}
?>
答案 0 :(得分:1)
我们没有看到您的SQL查询,但您应该使用AS
关键字为不明确的列提供列别名,如:
SELECT
t1.name AS t1name,
t2.name AS t2name
FROM
t1
JOIN t2 ON t1.id = t2.id
然后可以通过别名在提取的行中访问它们:
while ($joke = mysql_fetch_array($jokes)) {
echo "Table 1 name: {$joke['t1name']}\n";
echo "Table 2 name: {$joke['t2name']}\n";
}
进一步的建议(尽管我们没有看到它可能没有实际意义,因为它可能是没有意义的),是明确你选择哪些列。
而不是做
SELECT t1.*, t2.*
更好的做法是明确列出您需要的列:
SELECT t1.name, t2.id, t2.category .... t2.whatever
答案 1 :(得分:0)
这可以通过对$ jokes中的sql稍作修改来完成
SELECT j.name as jokename, a.name as authorname
FROM joke j, author a
WHERE j.author_id = j.id
然后,您可以通过$ joke ['jokename']和作者姓名列通过$ joke ['authorname']
引用笑话的名称列