Display data from several mysql tables in a single window?

Discussion in 'PHP' started by mynameisdi, Mar 4, 2009.

  1. #1
    So let's say if I want to display results from a single mysql table, the code is simple and looks something like this:

    
    <?php
    
    
    
    
    $data = mysql_query("SELECT * FROM video_comments ORDER BY id DESC LIMIT 5");
    while($row = mysql_fetch_array($data))
    {
    
    echo "
    ".$row[text]."
    
    
    
    ";
    
    }
    
    
    
    
    ?>
    
    PHP:
    But what if I want to agreggate data from several mysql tables and display them in a single results window?

    Any idea on how to do this?

    Many thanks in advance
     
    mynameisdi, Mar 4, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Do you literally mean what you posted? Display stuff from table 1, then stuff from table 2, then from table 3, all in the same page? The answer seems so obvious that it's painful to give it.

    Or do you mean interspersing the data from different tables based on some overarching sort criteria? In that case, you could combine the data in your SQL query or in PHP. Which one is more efficient depends on what your tables are like. More information is needed to answer that question.
     
    SmallPotatoes, Mar 4, 2009 IP
  3. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #3
    By creating 2 mysql queries ? Sometimes it's better to turn on your brains rather than posting silly questions :)
     
    ActiveFrost, Mar 4, 2009 IP
  4. qualityfirst

    qualityfirst Peon

    Messages:
    147
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I think he meant databases.

    If so, you're going to have to select a database, execute query, select the second database, execute query, and so on.
     
    qualityfirst, Mar 4, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    With MySQL you can do a union query across multiple databases.
     
    SmallPotatoes, Mar 4, 2009 IP
  6. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    could you not make do a JOIN 3 tables to the left of each other creating one big table then query it ??
     
    Funk-woo10, Mar 6, 2009 IP