MySQL、PHP多对多关系
我在人和兴趣之间有多对多的关系。这意味着我必须创建一个名为 person_interests 的中间表。
Person 表是使用以下命令创建的:
create table if not exists Person
(
id int not null auto_increment primary key,
username varchar(10) not null,
name varchar(40) not null,
gender varchar(6) not null,
dateOfBirth timestamp not null,
signUpDate timestamp not null,
email varchar(40) not null
);
兴趣表是使用以下命令创建的:
create table if not exists interests
(
id int not null auto_increment primary key,
interestName varchar(10) not null
);
insert into interests(id, interestName)
values
(1, 'sport'),
(2, 'computer'),
(3, 'dancing'),
(4, 'boating'),
(5, 'car');
person_interests 是使用以下命令创建的:
create table if not exists person_interests
(
id int not null auto_increment primary key,
UserID int not null,
foreign key (UserID) references Person(id),
Interestid int not null,
foreign key (Interestid) references interests(id)
);
我试图从 person_interests 表中选择条目,但没有成功。我执行此操作的 PHP 函数是:
function get_person_interests($id)
{
$connection = mysql_open();
$query = "select pi.UserID, i.interestName from interests as i, person_interests as pi, where i.id = pi.interestid";
$result = mysql_query($query, $connection) or show_error();
$person_interests = array();
while($person_interest = mysql_fetch_array($result))
{
$person_interests = $person_interest;
}
mysql_close($connection) or show_error();
return $person_interests;
}
但这不起作用!这就是我的模板 (user_detail.tpl) 的样子:
{foreach $interests as $interest}
<li>{$interest.interestName}</li>
{/foreach}
这就是我的 user_detail.php 的样子:
$id = $_GET['id'];
$Person = get_person($id);
$interests = get_person_interests($id);
$smarty = new Smarty;
$smarty->assign("Person", $Person);
$smarty->assign("interests", $interests);
$smarty->display("user_detail.tpl")
我试图显示用户在注册时选择的兴趣。它们存储在 person_interests 表中。每次我尝试访问 user_detail.php 页面时,都会出现以下错误:
Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where i.id = pi.interestid' at line 1
如果有人可以帮助我,那么我将不胜感激!
I've got a many-to-many relationship between Person and Interests. This means that I had to create a middle table called person_interests.
The Person table was created using:
create table if not exists Person
(
id int not null auto_increment primary key,
username varchar(10) not null,
name varchar(40) not null,
gender varchar(6) not null,
dateOfBirth timestamp not null,
signUpDate timestamp not null,
email varchar(40) not null
);
The interests table was created using:
create table if not exists interests
(
id int not null auto_increment primary key,
interestName varchar(10) not null
);
insert into interests(id, interestName)
values
(1, 'sport'),
(2, 'computer'),
(3, 'dancing'),
(4, 'boating'),
(5, 'car');
and person_interests was created using:
create table if not exists person_interests
(
id int not null auto_increment primary key,
UserID int not null,
foreign key (UserID) references Person(id),
Interestid int not null,
foreign key (Interestid) references interests(id)
);
I'm trying to select entries from the person_interests table but with no luck. My PHP function for doing this is:
function get_person_interests($id)
{
$connection = mysql_open();
$query = "select pi.UserID, i.interestName from interests as i, person_interests as pi, where i.id = pi.interestid";
$result = mysql_query($query, $connection) or show_error();
$person_interests = array();
while($person_interest = mysql_fetch_array($result))
{
$person_interests = $person_interest;
}
mysql_close($connection) or show_error();
return $person_interests;
}
But this doesn't work! This is what my template (user_detail.tpl) looks like:
{foreach $interests as $interest}
<li>{$interest.interestName}</li>
{/foreach}
and this is what my user_detail.php looks like:
$id = $_GET['id'];
$Person = get_person($id);
$interests = get_person_interests($id);
$smarty = new Smarty;
$smarty->assign("Person", $Person);
$smarty->assign("interests", $interests);
$smarty->display("user_detail.tpl")
I'm trying to display the interests that the user chose upon signup. They are stored in the person_interests table. Every time I try to go to the user_detail.php page it gives me this error:
Error 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where i.id = pi.interestid' at line 1
If anybody can help me then that would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQL 语法错误。
where
子句之前不应有逗号。我建议您将查询写为联接:当然,如果您想要特定人的兴趣(您的代码建议),您需要过滤 user_id:
回复您的评论:
我认为您想要将此行替换
为:
SQL syntax error. You shouldn't have a comma before the
where
clause. I would suggest that you write your query as a join though:Of course, if you want the interests of a particular person (Which your code suggests), you need to filter on the user_id:
In reply to your comment:
I think you want to replace this line:
With:
看起来像一个错字。您的表中有“Interestid”,但查询中有“interestid”。注意大小写的区别。你的where后面有一个逗号。您应该使用连接重写它:
Looks like a typo. You have "Interestid" in your table, but "interestid" in your query. Note the case difference. And you have a comma after your where. You should rewrite this using a join by doing:
您的 SQL 查询中有错误,您应该首先调试该错误。
使用命令行客户端连接到数据库(该命令名为 mysql)。
然后在文本编辑器中编写 SQL 查询。然后复制查询并将其粘贴到命令行客户端中以运行它。
命令行客户端总是会告诉您错误在哪里。然后,您可以阅读 MySQL 手册中的 SQL 语句,并查看哪里出现语法错误或类似错误。
然后您可以在编辑器中相应地采用查询并重试。
重复此操作,直到您的查询不再出现错误并获得预期的结果。
然后将工作查询复制到您的应用程序中,并用变量替换不变量。请注意正确转义这些值。
You have an error in your SQL query which you should debug first.
Connect to your database with a commandline client (the command is named mysql).
Then write the SQL query in a text editor. Then copy the query and paste it into the commandline client to run it.
The commandline client will always tell you where the error is. You can then read the SQL statement in the MySQL manual and look where you've made a syntax error or something similar.
Then you can adopt the query in your editor accordingly and try again.
Repeat until your query has no error any longer and gets you the expected results.
Then copy the working query into your application and replace the invariants with the variables. Take care that you properly escape the values.