By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Database Table Display Problem

ribbo
P: 9
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("(SELECT * FROM cars) UNION (SELECT * FROM truck) order by date desc, time desc");
while($row=mysql_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 :)
Feb 19 '08 #1
Share this Question
Share on Google+
5 Replies


Markus
Expert 5K+
P: 6,050
and what output does that give you?
Feb 19 '08 #2

ribbo
P: 9
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.
Feb 19 '08 #3

ribbo
P: 9
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.

:(
Feb 19 '08 #4

ribbo
P: 9
What an idiot. i have now figured out my own problem due to the error code.

Lesson learned!
Feb 19 '08 #5

Markus
Expert 5K+
P: 6,050
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 ;)
Feb 19 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.