SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    display database of multiple table

    I query 2 tables, table 1 gives back one row, table 2 gives back somewhere from 1 to 10 rows

    Normally I would do a while loop mysql_fetch_array the query result, then just echo every fields out.

    But because the html, where the data will be displayed in, is too big. And I don't want to make the hmlt into php.

    So I am asking for a solution that can distribute(echo) data from that query to the html without using while loop or using while loop by a technique that I didn't know.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    use a join query instead of a while loop, this will allow you to return everything at once
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    use a join query instead of a while loop, this will allow you to return everything at once
    You mean naming the fields in table 2 in the join query ?

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even though the answer is yes, I have no idea how I would do that, I am gonna need you tailor the solution for this situation.

    listing(id, title, detail)

    listing_photo(listing_id, path)

    The query always give back one row from table listing. The number of rows in table listing_photo vary from time to time as one user can upload more than one photo for one listing.

    I need the row from listing and all possible rows from listing_photo

    My WHERE clause is " WHERE id = {$listing}"

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2008
    Posts
    192
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Read it
    mysql join

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,216
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT l.title
         , l.detail
         , lp.path
      FROM listing AS l
    LEFT OUTER
      JOIN listing_photo AS lp
        ON lp.listing_id = l.id
     WHERE l.id = {$listing}
    run that a couple of times outside of php first, to see what it returns, using hardcoded id numbers
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, this is how i would do it, and I don't know hardcoded id numbers

    Code PHP:
    $listing_id = 238;
    $query = "SELECT l.title
    		 , l.detail
    		 , lp.path
    	FROM listing AS l
    	LEFT OUTER
    	JOIN listing_photo AS lp
        ON lp.listing_id = l.id
    	WHERE l.id = {$listing_id}";
     
    $result= mysql_query($query);
    $rows = mysql_fetch_array($result);
    echo $rows['title'];
    echo $rows['detail'];
    echo $rows['path'];
    ?>

    I have one title back, which is right.

    I have one detail back, which is right.

    I have one path back, which is not desirable. Now I need you to show the technique to echo rest of path


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
  •