meames Posted May 21, 2009 Share Posted May 21, 2009 Hi all, Not sure if this fits in the PHP or MySQL section, I guess its more PHP though so here goes......I am currently trying to write a search script which takes a search term from the user and also takes 3 options on what to search for depending on what the user wants: Keywords Story Title Main Story I have passed all of this to a new page which has the following query in: $Query = "SELECT * FROM News WHERE Park LIKE '%$SearchTerm%'"; The 'Park' will be searched every time, but I only want to search on the other options if the user has selected them, I thought about adding the following: if(!empty($Keywords)) { $Query = $Query + "OR Keywords LIKE '%$SearchTerm%'"; } and so on for the other 2 options, yet this does not work. There must be some way of doing this, can anybody help? Thanks. Link to comment https://forums.phpfreaks.com/topic/159078-solved-joining-2-select-statements-depending-on-condition/ Share on other sites More sharing options...
Axeia Posted May 21, 2009 Share Posted May 21, 2009 Try echo'ing out the query, think you're missing a space before the OR. After echo'ing it out try executing it in phpmyadmin or whatever tool you have available to see if it works. Link to comment https://forums.phpfreaks.com/topic/159078-solved-joining-2-select-statements-depending-on-condition/#findComment-838927 Share on other sites More sharing options...
meames Posted May 21, 2009 Author Share Posted May 21, 2009 Thanks, echo'ing out the query led went to show me what was going off. I have now got it working with the following code: $Query = "SELECT * FROM News WHERE Park LIKE '%$EditedSearchTerm%'"; if(!empty($Keywords)) { $Query = "$Query OR Keywords LIKE '%$EditedSearchTerm%'"; } Really happy over this, I can move on now Link to comment https://forums.phpfreaks.com/topic/159078-solved-joining-2-select-statements-depending-on-condition/#findComment-838931 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.