elBadr
elBadr

Reputation: 25

Select all from TWO tables

I'm trying to select all the results from a table A and all results from table B
I used this Request:

$request_public_imgs = $BDD-> query("SELECT * FROM images_public ");
    UNION("SELECT * FROM images_users  WHERE img_kind='PublicImg' ")
    ;

But I receive this error

Fatal error: Call to undefined function UNION()

Upvotes: 0

Views: 20657

Answers (3)

Adrien Hadj-Salah
Adrien Hadj-Salah

Reputation: 206

Write your SQL command as a single piece :

$request_public_imgs = $BDD-> query("SELECT id, field1, .. FROM images_public 
   UNION SELECT id, field1, .. FROM images_users  WHERE img_kind='PublicImg' ") ;

You should select precise field and not using * for performance matter. Adrien

Upvotes: 4

Gar
Gar

Reputation: 862

try this

$request_public_imgs = $BDD-> query("SELECT * FROM images_public 
    UNION SELECT * FROM images_users  WHERE img_kind='PublicImg' ")
    ;

Upvotes: 0

fedorqui
fedorqui

Reputation: 290525

UNION is a MySQL function, hence you need to put it inside the query():

$request_public_imgs = $BDD-> query("
SELECT * FROM images_public
    UNION
SELECT * FROM images_users  WHERE img_kind='PublicImg' ");

More generally, all the queries will work like:

$request_public_imgs = $BDD-> query("YOUR_MYSQL_QUERY");

Note that the error Fatal error: Call to undefined function UNION() was warning about UNION not being a function of PHP.

Upvotes: 3

Related Questions