Results 1 to 7 of 7

Thread: Showing mysql in table

  1. #1
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Question Showing mysql in table

    Hi,

    I have 2 tables;
    1) name, last name.
    2) last name, shirt-color.

    I want to print (as html) a table that shows:
    name | last name | shirt-color (asuming each name has other last name)

    I just don't know how to print a table which combines 2 tables.

    Any ideas?

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Try something like:

    Code:
    SELECT
        table1.name,
        table1.lastname,
        table2.shirtcolor
    FROM 
        table1,
        table2
    WHERE 
        table1.lastname = table2.lastname;
    Take a look at this, as well. You may find it helpful. It starts with basic queries, which I am sure you already know, and goes up to more complex ones.
    Last edited by Jas; 08-08-2008 at 03:27 AM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. The Following User Says Thank You to Jas For This Useful Post:

    d-machine (08-08-2008)

  4. #3
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    Hi Jas and d-machine I've tried to do it too (for practice),

    That's what I've tried to do:
    PHP Code:
    $result mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
    WHERE mytable1.lastname = mytable2.lastname;"
    );

    echo 
    "<table border='1'>";
    echo 
    "<tr>  <th>name</th> <th>lastname</th> <th>shirtcolor</th> </tr>";
    // keeps getting the next row until there are no more to get
    while($row mysql_fetch_array$result )) {
        
    // Print out the contents of each row into a table
        
    echo "<tr><td>"
        echo 
    $row['mytable1.name'];
        echo 
    "</td><td>"
        echo 
    $row['mytable1.lastname'];
        echo 
    "</td><td>"
        echo 
    $row['mytable2.shirtcolor'];
        echo 
    "</td></tr>"


    echo 
    "</table>"
    for some reason it printed me an empty table (with the correct size of rows and columns).

  5. The Following User Says Thank You to lord22 For This Useful Post:

    d-machine (08-08-2008)

  6. #4
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you lord22 and Jas,
    I've tried your both Ideas and actually got stuck exactly in the same problem as lord22..

  7. #5
    Join Date
    Jul 2007
    Location
    Azerbaijan, Baku
    Posts
    144
    Thanks
    11
    Thanked 27 Times in 25 Posts

    Default

    Change this:

    Code:
    $result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
    WHERE mytable1.lastname = mytable2.lastname;");
    To this:

    Code:
    $result = mysql_query("SELECT mytable1.name, mytable1.lastname, mytable2.shirtcolor FROM mytable1, mytable2
    WHERE mytable1.lastname = mytable2.lastname;") or die(mysql_error());
    And look at the error.

  8. The Following User Says Thank You to allahverdi For This Useful Post:

    d-machine (08-09-2008)

  9. #6
    Join Date
    Aug 2008
    Location
    Worldwide
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Also try removing the semi-colon at the end of the query

    mytable2.lastname;");

  10. The Following User Says Thank You to Nightfire For This Useful Post:

    d-machine (08-09-2008)

  11. #7
    Join Date
    Nov 2007
    Posts
    151
    Thanks
    67
    Thanked 0 Times in 0 Posts

    Default

    Thank you, I've fixed my errors, and now it works fine!

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
  •