UNION Problem!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

UNION Problem!

Post by Joe »

I am trying to select data from another table using UNION ALL SELECT but I seem to be getting continuous errors saying, You have an error in your SQL syntax near 'UNION ALL SELECT products FROM shopping_cart' at line 1. This is my code:

Code: Select all

<?php
$sql = "UNION ALL SELECT products FROM shopping_cart";
$res = mysql_query($sql) or die(mysql_error());

if (mysql_num_rows($res))
{
  $row = mysql_fetch_assoc($res);
  echo $row['products'];
}
Does anyone see any errors in my code that could be repaired. Thanks!


Joe 8)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

mysql manual wrote:(SELECT a FROM tbl_name WHERE a=10 AND B=1 ORDER BY a LIMIT 10)
UNION
(SELECT a FROM tbl_name WHERE a=11 AND B=2 ORDER BY a LIMIT 10)
ORDER BY a;
User avatar
launchcode
Forum Contributor
Posts: 401
Joined: Tue May 11, 2004 7:32 pm
Location: UK
Contact:

Post by launchcode »

UNION is only available as of MySQL 4.x and above - which is always worth checking (my host still uses 3.x for example) - also in your given example I can't even see the need for a union anyway as you only seem to be bringing back one table - but that might just be the way you wrote it. If all you want to do is bring back everything from 2 un-related tables (that cannot be joined) it's just:

Code: Select all

SELECT a, b, c FROM table_one
UNION
SELECT a, b, c FROM table_two;
Post Reply