字段列表中的列是不明确的MYSQL、PHP、搜索、选择
在成功连接到数据库后,我尝试使用以下语句从mysql数据库中选择信息:字段列表中的列是不明确的MYSQL、PHP、搜索、选择,php,mysql,mysql-error-1052,Php,Mysql,Mysql Error 1052,在成功连接到数据库后,我尝试使用以下语句从mysql数据库中选择信息: $query = "SELECT * FROM `users`, `markers`,`imagemarkers` WHERE username LIKE '%$s%' OR email LIKE '%$s%' OR location LIKE '%$s%' OR author LIKE '%$s%' OR bike LIKE '%$s%' OR id LIKE '
$query = "SELECT * FROM `users`, `markers`,`imagemarkers`
WHERE username LIKE '%$s%'
OR email LIKE '%$s%'
OR location LIKE '%$s%'
OR author LIKE '%$s%'
OR bike LIKE '%$s%'
OR id LIKE '%$s%'
OR title LIKE '%$s%'";
我得到了这个错误:where子句中的“author”列是不明确的。我之所以理解它,是因为我有多个具有相同字段名的表
我想从这些表和这些字段中提取信息:
-markers:
author
title
bike
id
date
-imagemarkers:
-author
-title
-id
-date
-users:
-loction
-email
-username
-id
我已经寻找了解决方案,到目前为止,我得出的结论是,每个表字段都应该被称为:
markers.title
imagemarkers.title
markers.author
imagemarkers.author
markers.date
imagemarkers.date
markers.id
imagemarkers.id
users.id
这句话可能看起来像:
SELECT markers.author
FROM markers JOIN imagemarkers ON markers.author = imagemarkers.author
但我不知道如何利用我需要检索的大量信息来实现这一点
我目前拥有的全部代码如下所示:
if (isset($_POST['submit'])) {
if ($_POST['search'] !="") {
require("connection.php");
$s = mysql_real_escape_string($_POST['search']);
$query = "SELECT * FROM `users`, `markers`,`imagemarkers` WHERE username LIKE '%$s%' OR email LIKE '%$s%' OR location LIKE '%$s%' OR author LIKE '%$s%' OR bike LIKE '%$s%' OR id LIKE '%$s%' OR title LIKE '%$s%'";
$result = mysql_query($query, $connect)
or die(mysql_error());
$num = mysql_num_rows($result);
echo "<h2>you searched for: " . $s . "..</h2>";
echo "<h5>there are " . $num . " results</h4>";
while ($row = mysql_fetch_assoc($result)) {
echo "<p>username: " . $row['username'] . "<br />";
echo "location: " . $row['location'] . "</p>";
echo "author: " . $row['author'] . "</p>";//error: Column 'author' in where clause is ambiguous, same with date.
echo "date: " . $row['date'] . "</p>";
echo "id: " . $row['id'] . "</p>";
echo "title: " . $row['title'] . "</p>";
echo "<hr />";
}
} else {
echo "<h3> you must type something in the box</h3>";
} }
if(isset($\u POST['submit'])){
如果($_POST['search']!=“”){
要求(“connection.php”);
$s=mysql\u real\u escape\u字符串($\u POST['search']);
$query=“从`users`、`markers`、`imagemarkers`中选择*,其中用户名为“%$s%”,电子邮件为“%$s%”,位置为“%$s%”,作者为“%$s%”,自行车为“%$s%”,id为“%$s%”,标题为“%$s%”;
$result=mysql\u查询($query,$connect)
或者死(mysql_error());
$num=mysql\u num\u行($result);
echo“您搜索的:”.$s.“…”;
回显“有”。$num.“结果”;
while($row=mysql\u fetch\u assoc($result)){
echo“用户名:”.$row['username']。“
”;
echo“位置:”.$row[“位置”]。“”;
echo“author:”.$row['author'].“”;//错误:where子句中的“author”列不明确,与date相同。
回显“日期:.”行['date']。“”;
echo“id:.”行['id']。“”;
回声“标题:.”行['title']。“”;
回声“
”;
}
}否则{
echo“您必须在框中键入内容”;
} }
有人能帮我吗
非常感谢 对于一个简单的问题来说,代码太多了。。“不明确”表示您选择的案例或案例中的标识符出现在多个表中。。如果您说的是“选择作者”,则需要指定来自哪个表。。那么,作者们。如果你在做“where authors…”,你需要“where markers.authors”
我建议您合并您的表格。对于一个简单的问题,这需要很多代码。。“不明确”表示您选择的案例或案例中的标识符出现在多个表中。。如果您说的是“选择作者”,则需要指定来自哪个表。。那么,作者们。如果你在做“where authors…”,你需要“where markers.authors”
我建议合并您的表。这是因为字段的命名方式相同 尝试:
依此类推,这是因为字段的命名方式相同 尝试:
等等又快又脏
$query = "SELECT * FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id)
WHERE u.username LIKE '%$s%'
OR u.email LIKE '%$s%'
OR u.location LIKE '%$s%'
OR m.author LIKE '%$s%'
OR m.bike LIKE '%$s%'
OR m.title LIKE '%$s%'";
这将把结果链接在一起,以便只显示与用户相关的标记和图像标记
请注意,我不知道markers.author是链接到users.id
的整型字段还是链接到用户的char字段。name
此查询假设为前者,请相应调整连接条件,因为这与表不匹配
如果不考虑连接条件,则最终会出现交叉连接,这99%可能不是您想要的。
见:)
明确列出列
如果要限制列,必须显式命名(推荐)
请注意,由于所有内容都链接到author上,因此只需列出该字段一次,否则您将得到3个内容完全相同的authorfields,这将是愚蠢的。快速且肮脏
$query = "SELECT * FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id)
WHERE u.username LIKE '%$s%'
OR u.email LIKE '%$s%'
OR u.location LIKE '%$s%'
OR m.author LIKE '%$s%'
OR m.bike LIKE '%$s%'
OR m.title LIKE '%$s%'";
这将把结果链接在一起,以便只显示与用户相关的标记和图像标记
请注意,我不知道markers.author是链接到users.id
的整型字段还是链接到用户的char字段。name
此查询假设为前者,请相应调整连接条件,因为这与表不匹配
如果不考虑连接条件,则最终会出现交叉连接,这99%可能不是您想要的。
见:)
明确列出列
如果要限制列,必须显式命名(推荐)
请注意,由于所有内容都链接到author上,因此只需列出该字段一次,否则您将得到3个内容完全相同的authorfields,这将是愚蠢的。非常感谢Flask,它工作正常,echo的所有数据库内容:)您确实意识到这是一个交叉连接,这是您想要的吗,因为来自表格用户
,标记
,图像标记
的结果将完全没有关系。@corse的Johan。我刚才回答了一个问题,为什么它模棱两可,以及如何绕过它。当然,带有连接的解决方案要干净得多,但正如您所看到的,他没有提供任何有关其表的信息。顺便说一句,选择像这样的ID或像“%$s%”这样的m.author不是很愚蠢吗?
?非常感谢,它工作得很好,echo是数据库中的所有内容:)您确实意识到这是一个交叉连接,这就是您想要的,因为来自表用户,标记的结果,imagemarkers
将彼此完全没有关系。@corse的Johan。我刚才回答了一个问题,为什么它模棱两可,以及如何绕过它。当然,带有连接的解决方案要干净得多,但正如您所看到的,他没有提供任何有关其表的信息。顺便说一下,选择像这样的ID或像“%$s%”这样的m.author不是很愚蠢吗?
$query = "SELECT u.location, u.email, u.username as author, u.id as user_id
,m.title as marker_title, m.bike, m.date as marker_date, m.id as marker_id
,im.title as imagemarker_title, im.date as imagemarker_date, im.id as imagemarker_id
FROM `users` u
INNER JOIN `markers` m ON (m.author = u.id)
INNER JOIN``imagemarkers` im ON (im.author = u.id)
WHERE u.username LIKE '%$s%'
OR u.email LIKE '%$s%'
OR u.location LIKE '%$s%'
OR m.author LIKE '%$s%'
OR m.bike LIKE '%$s%'
OR m.title LIKE '%$s%'";