开发者

SQL ERROR When i join 2 tables

开发者 https://www.devze.com 2023-04-07 04:20 出处:网络
Sorry let me revise. I have a three tables: events_year • EventID • YearID • id Date • YearID • Year

Sorry let me revise. I have a three tables:

events_year

• EventID

• YearID

• id

Date

• YearID

• Year

Event

• EventID

• EventName

• EventType

i want to dispay a record from the three tables like so:

EventName - Year: Marathon - 2008

i linked it to a table called "members" which contains a ID number field (members-id)

so i can limit the results to members id = $un(which is a username from a session)

I need to join the three tables and limit the results to the specific ID number record

Here is my portion of the code:

$query =    "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";
            "开发者_开发知识库SELECT * FROM Event JOIN events_year ON Event.EventID = events_year.EventID WHERE username = '$un'";
            "SELECT * FROM Date JOIN events_year ON Date.YearID = events_year.YearID WHERE username = '$un'";


$results = mysql_query($query)
    or die(mysql_error());

    while ($row = mysql_fetch_array($results)) {
    echo $row['Year'];
    echo " - ";
    echo $row['Event'];
    echo "<br>";
    }       


the notices are almost self-explaining. There are no 'Year' and 'EventName' fields in the resultset. It's difficult (or: impossible) to tell why this happens as you haven't given your table-structure, but i guess this: 'Year' is a field of the date-table, 'EventName' is a field of the event-table - you're only selecting from members so this fields don't occur.

I don't understand why there are three sql-statements but only one is assigned to a variable - the other two are just standing there and do nothing. Please explain this and put more information into your question about what you're trying to achive, what your table-structure looks like and whats your expected result.

I think what you really wanted to do is some kind of joined query, so please take a look at the documentation to see how this works.

finally, i think your query should look like this:

SELECT
  *
FROM
  members
INNER JOIN
  events_year ON members.id = events_year.id
INNER JOIN
  Event ON Event.EventID = events_year.EventID
INNER JOIN
  ´Date´ ON ´Date´.YearID = events_year.YearID
WHERE
  members.username = '$un'


Does the field 'Year' exist in the query output ? I suspect not.


the string $query is only using the first line of text:

    "SELECT * FROM members JOIN events_year ON members.id = events_year.id ";

and not the others.

The query itself is not returning any fields that are called Year or EventName.

Do a var_dump($row) to find out what is being returned.

0

精彩评论

暂无评论...
验证码 换一张
取 消