Database Table Display Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ribbo
    New Member
    • Jan 2008
    • 9

    Database Table Display Problem

    Hello everyone,

    I have been trying to solve this problem for too long now, and was hoping someone maybe able to help me solve my problem.

    I basically want to be able to UNION 2 tables together and order the contents by date and time.

    i.e.

    Honda Civic 01/02/08 10:45am (From database CARS)
    Chevy Cobalt 01/02/08 11:45am (From database CARS)
    Ford F-50 02/03/08 1:05pm (From database TRUCK)

    [php]
    <?php

    require('config .php');

    $result = mysql_query("(S ELECT * FROM cars) UNION (SELECT * FROM truck) order by date desc, time desc");
    while($row=mysq l_fetch_array($ result))
    {
    $make=$row["make"];
    $model=$row["model"];
    $date=$row["date"];
    $time=$row["time"];

    echo "$make";
    echo "$model";
    echo "$date";
    echo "$time";
    }

    ?>
    [/php]

    I appreciate any help :)
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    and what output does that give you?

    Comment

    • ribbo
      New Member
      • Jan 2008
      • 9

      #3
      hey, all i get is a blank screen.

      I have data in the tables, and i can display 1 table at a time if i take out [php]UNION (SELECT * FROM truck)[/php] etc..

      I just don't get it.

      Comment

      • ribbo
        New Member
        • Jan 2008
        • 9

        #4
        Really really sorry about this.

        MySQL Error: The used SELECT statements have a different number of columns

        i just read another post from a moderator and said to not forget to add the error handling.

        :(

        Comment

        • ribbo
          New Member
          • Jan 2008
          • 9

          #5
          What an idiot. i have now figured out my own problem due to the error code.

          Lesson learned!

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Try doing
            [php]
            SELECT `col1`,`col2`,` col3` FROM `table_1`
            UNION
            SELECT `col1`, col2`, col3` FROM `table_2`
            [/php]
            Or something like that.. not sure.

            EDIT: Just saw your last post... Silly me!

            Post up your revised code; this could help someone else ;)

            Comment

            Working...