0

Here is a piece of code in pdo:

$result = $pdo->prepare("SELECT * FROM `games_server` WHERE `full_address` = :fulladdress LIMIT 1");
$result->execute(array('fulladdress'=>$address));

What I need to do is to union this select with another table like

(SELECT * FROM `games_server` WHERE `full_address` = :fulladdress LIMIT 1) UNION (SELECT * FROM `x_servers` WHERE `full_address` = :fulladdress LIMIT 1)

, but there seems to be a syntax error and also these tables have a different number of columns, so it seems impossible.

My question is if there's any way around this. I've looked around and tried a bunch of variants and nothing worked.

Does anybody have any clues how to make it work?

  • 1
    Thing you have to use `:fulladdress1` and `:fulladdress2` and bind both params. – JOUM Sep 18 '16 at 18:13
  • Thank you very much for the answer, but could you please explain how to bind them? – Maxim K. Magnes Sep 18 '16 at 18:17
  • What does it mean, " there seems to be a syntax error"? If there is an error, what does it say? – Your Common Sense Sep 18 '16 at 18:21
  • well when I try it in phpmyadmin it says #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':fulladdress1 LIMIT 1) UNION (SELECT * FROM `x_servers_ban` WHERE `full_address`' at line 1 actual script doesn't give any errors but it just doesn't work – Maxim K. Magnes Sep 18 '16 at 18:22
  • `array(':fulladdress1'=>$address,':fulladdress2'=>$address)` like that – JOUM Sep 18 '16 at 18:23
  • Note that you cannot use union query with different number of columns. select only equal columns from both tables – Your Common Sense Sep 18 '16 at 18:26
  • Thank you everyone. This is the solution: (SELECT `full_address` as `full_address_result` FROM `games_server` WHERE `full_address` = :fulladdress LIMIT 1) UNION (SELECT `full_address` as `full_address_result` FROM `x_servers` WHERE `full_address` = :fulladdress LIMIT 1) – Maxim K. Magnes Sep 18 '16 at 18:36

0 Answers0