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.'%');
Edited by zid
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.