Jump to content

Cannot search mutiple tables


zid

Recommended Posts

Hi guys, trying to acchieve a search query on mutiple tables using prepared statements and the query it self seems a bit confusing for me, have been searching and found the UNION feature but I still cannot get this to work. Please advice.

 

pages

- page.title

- page.text

 

posts

- post.title

- post.text

 

 

This one works, single table using two rows:

$stmt = $db->prepare("SELECT * FROM pages WHERE page_title LIKE :search_string OR page_text LIKE :search_string");

This one does not work with the UNION feature:

$stmt = $db->prepare("
   SELECT * FROM pages.page_title, WHERE pages.page_title LIKE :search_string
   UNION
   SELECT * FROM pages.page_text, WHERE pages.page_text LIKE :search_string
   UNION
   SELECT * FROM posts.post_title, WHERE posts.post_title LIKE :search_string
   UNION
   SELECT * FROM posts.post_text, WHERE posts.post_text LIKE :search_string
   ");

And the bound:

$stmt->bindValue(':search_string', '%'.$query.'%');
Link to comment
https://forums.phpfreaks.com/topic/288033-cannot-search-mutiple-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.