dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Getting Data From a Foreign Key

  1. #1
    Join Date
    Feb 2006
    Posts
    15

    Getting Data From a Foreign Key

    I have two tables:

    tblArticle
    ---------------
    ArticleID(PK)
    ArticleContent
    CategoryID(FK)

    tblcategory
    --------------
    CategoryID(PK)
    CategoryName

    I wanted to know how to create mysql_query that can get ArticleContent, CategoryName FROM tblArticle without accessing the tblcategory?

    I appreciate any type of help....

  2. #2
    Join Date
    Jan 2006
    Location
    London
    Posts
    50
    The only way you can return CategoryName without accesing tblcategory is to move that field into tblArticle.

    However this destroys the whole purpose of having seperated it into another table in the first place, and assuming that you have created two tables due to a one to many or many to many relationship will result in a lot of data duplication.

    The query you need will access both tables at the same time:

    SELECT A.ArticleContent, C.CategoryName
    FROM tblArticle A, tblcategory C
    WHERE A.CategoryID(FK) = C.CategoryID(FK);

    When this is returned in PHP you reference the resultset with out the post fix i.e.

    $row['ArticleContent'];
    $row['CategoryName'];

    Mr B

    P.S. in the SQL you can replace the A and the C with anything you like as long as it is the same throughout the query.
    Last edited by Brooksie155; 02-10-2006 at 05:47 AM.

  3. #3
    Join Date
    Feb 2006
    Posts
    15

    yes & no

    i think i understand but what do you mean by replacing A & C with anything...

    can you give me an example

  4. #4
    Join Date
    Feb 2006
    Posts
    15

    tq bro

    nvw i get it now...thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles