SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    selecting from 2 tables?

    Hi,

    I am creating a page where the user can edit their database. When the user clicks on the link to the book it brings up the relevant fields in the editing form. (using $_get['id']) for book value. However the problem is this. One of the fields is a foriegn key to the category table (cat_id). So this shows in the textbox as an integer. What I wish to show is the category name. (from the category table.) The core function i am using is below:

    PHP Code:
    function get_books_by_id($subject_id){
        global 
    $connection;    
        
    $query "SELECT * FROM books WHERE isbn = '$subject_id'";     
        
    $result=mysqli_query$connection$query);
        
    confirm_query($result);    
        if(
    $subject mysqli_fetch_array($result)){        
        return 
    $subject;
        }else{
        return 
    NULL;
    }

    I have been bursting my brains trying to get round this problem but I feel I am digging myself an early grave. Anybody got any ideas how I can download the catname to this field by maybe altering the above function?

    Thanks for your time

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    67 Post(s)
    Tagged
    2 Thread(s)

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT books.*
         , category.name AS cat_name
      FROM books
    INNER
      JOIN category
        ON category.id = books.cat_id
     WHERE books.isbn = '$subject_id'

  4. #4
    SitePoint Evangelist
    Join Date
    May 2007
    Location
    Kent in Uk
    Posts
    538
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    I got it to work using a simple join on the tutorial page you put up. However this is all quite new to me so thanks for the link. I shall dig deeper into the hidden depths of mysql.

    crmalibu - thanks for that. Again inner joins are a bit of mystery to me at the moment so im going to bury my head in a few tutorials

    cheers


Bookmarks

Posting Permissions

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