# php UNION problem

**URL:** <https://www.phphelp.com/t/php-union-problem/25478>\
**Category:** MySQL database\
**Created:** [September 20, 2016, 8:47am UTC](https://www.phphelp.com/t/php-union-problem/25478 "2016-09-20T08:47:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://www.phphelp.com/uploads/default/original/2X/2/243e9ed5d93c5653ce3186f18df16e019ce38445.png) [@system](https://www.phphelp.com/u/system)\
**Post date:** [September 20, 2016, 8:47am UTC](https://www.phphelp.com/t/php-union-problem/25478/1 "2016-09-20T08:47:12Z")

</div>

First of all I want to say Hi to all.

I have a strange problem with sql query in php.  
I’m using PDO to connect with MySQL database. A query:

“SELECT o.\* FROM table1 o UNION SELECT o.\* FROM table2 o LEFT JOIN table3 d ON o.field1 = d.field1 ORDER BY field2 ASC”

doesn’t work in php.

When I using this query in PHPMyAdmin it works fine.  
Any suggestions would be very appreciated.

Thanks in advance,

Kris

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [September 20, 2016, 1:01pm UTC](https://www.phphelp.com/t/php-union-problem/25478/2 "2016-09-20T13:01:08Z")

</div>

Something isn’t correct. Do you have PDO exception modes on? By default they hide exceptions.

[php]$pdo-\>setAttribute(PDO::ATTR\_ERRMODE, PDO::ERRMODE\_EXCEPTION);[/php]

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [September 20, 2016, 3:08pm UTC](https://www.phphelp.com/t/php-union-problem/25478/3 "2016-09-20T15:08:33Z")

</div>

Everytime I see someone using Union their database design is wrong. Post your DB schema. The database design needs to be right before you write any code.

---

<div class="post-metadata">

**Author:** ![astonecipher](https://www.phphelp.com/user_avatar/www.phphelp.com/astonecipher/32/2797_2.png) [@astonecipher](https://www.phphelp.com/u/astonecipher)\
**Post date:** [September 20, 2016, 3:53pm UTC](https://www.phphelp.com/t/php-union-problem/25478/4 "2016-09-20T15:53:44Z")

</div>

That are perfectly valid times to use union, just because you haven’t seen proper usage does not mean that it is a design issue.

---

<div class="post-metadata">

**Author:** ![benanamen](https://www.phphelp.com/user_avatar/www.phphelp.com/benanamen/32/287_2.png) [@benanamen](https://www.phphelp.com/u/benanamen)\
**Post date:** [September 20, 2016, 4:24pm UTC](https://www.phphelp.com/t/php-union-problem/25478/5 "2016-09-20T16:24:05Z")

</div>

On Help forums with inexperienced posters it has always been the wrong DB design. Best thing is for the OP to post his DB schema.
