php - How to retrieve certain value(s) through foreign key linking in MySQL? -


sorry unclear title couldn't come better.

my dilemma this:

i have 1 innodb table in database called "meetings" following structure:

  • meeting_id (primary key, auto_increment)
  • user1_id (foreign key pointing user_id in table called "users")
  • user2_id (foreign key pointing user_id in table called "users")
  • time (type datetime)
  • location (type varchar(200))

the table "users" basic , looks this:

  • user_id (primary key, auto_increment)
  • first_name (type varchar(30))
  • last_name (type varchar(30))

i have php file aim print out description of meeting, e.g.:

you saved following meeting information:

user 1 | user 2 | time & date | meeting location

john doe | jane doe | 2010-10-10 10:10:10 | new york

now, want use meeting id, call database (only "meetings" table) , able first_name , last_name of user1 , user2.

right now, non-working code looks this:

$query = "select * meetings meeting_id = " . $mid; $data  = mysqli_query($dbc, $query); $row   = mysqli_fetch_array($data); // changes here?  ...  echo '<p>you saved following meeting information::</p>'; echo '<table>'; echo '<tr><th>user 1</th><th>user 2</th><th>time & date</th><th>meeting location</th></tr>'; echo '<td>' . $row['user1_id']['first_name'] . ' ' . $row['user1_id']['last_name'] . '</td>'; // non-working echo '<td>' . $row['user2_id']['first_name'] . ' ' . $row['user2_id']['last_name'] . '</td>'; // non-working echo '<td>' . $row['date_time'] . '</td>'; echo '<td>' . $row['location'] . '</td>';        echo '</table>'; ... 

(how) can retrieve first_name/last_name links without making separate calls "users" table? when check phpmyadmin, innodb foreign key links seem work fine. many in advance!

if query be

$query = "select time, location, user1.first_name, user1.last_name, user2.first_name, user2.last_name meetings m join users user1 on m.user1_id = user1.id join users user2 on m.user2_id = user2.id meeting_id = " . $mid; 

then names should available

$row['user1.first_name'] 

and on. foreign keys not magical devices bring in related records (nor should be).

edit:
side note - having columns end numbers such user1_id , user2_id raise red flag in mind of people understand normalization , database design. boils down question - ready accept meetings support meetings between 2 people , 2 people only? question should answer is: sure want distinguish between 'first' , 'second' participant of meeting? (current design make harder answer questions such - list meetings $user. current table layout have test both fields separately, might hurt performance)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -