简体   繁体   中英

MySQL, PHP many-to-many relationship

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!

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:

$query = "select pi.UserID, i.interestName from person_interests as pi join interests as i on i.id = pi.interestid";

Of course, if you want the interests of a particular person (Which your code suggests), you need to filter on the user_id:

$query = "select pi.UserID, i.interestName from person_interests as pi join interests as i on i.id = pi.interestid where pi.UserID =" . mysql_escape_string($id);

In reply to your comment:

I think you want to replace this line:

$person_interests = $person_interest;

With:

$person_interests[] = $person_interest;

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:

select
    pi.UserId,
    i.interestname
from
    person_interests pi
left join
    interests i
on
    pi.Interestid = i.id

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM