garyneedham Posted July 7, 2011 Share Posted July 7, 2011 Hello can anyone help? I have the follow search which works very well but I want to do another search, I want to search 8 different fields for example serial1 > Serial8 please can you keep answer very simple and step by step as I am very new to this thanks in advance for any help $sql = "SELECT * FROM Customer WHERE Serial LIKE '%" . mysql_escape_string($_GET[serial']) . "%' ORDER BY Serial "; if (empty($_GET['Serial'])) $sql = "SELECT * FROM Nif WHERE 1 = 2 "; $res = mysql_query($sql); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/ Share on other sites More sharing options...
AyKay47 Posted July 7, 2011 Share Posted July 7, 2011 you will want to use the "AND" statement with your WHERE statement you can look here Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1239624 Share on other sites More sharing options...
fenway Posted July 8, 2011 Share Posted July 8, 2011 Not that it's particularly efficient.... Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1240067 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 Not that it's particularly efficient.... perhaps you can provide a solution rather than writing 5 words that don't help at all? It looks like i misunderstood the question and you're right, an AND statement would be very long and insufficient...OP perhaps you can better explain your situation. Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1240074 Share on other sites More sharing options...
fenway Posted July 8, 2011 Share Posted July 8, 2011 I simply meant that LIKE '%str%" doesn't take advantage of any index. Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1240078 Share on other sites More sharing options...
AyKay47 Posted July 8, 2011 Share Posted July 8, 2011 I simply meant that LIKE '%str%" doesn't take advantage of any index. I'm terribly sorry then Fenway, my apologies..I thought you were talking about my reply...however your reply did make me look back at the original post and realize that it is much more messy than I thought... Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1240081 Share on other sites More sharing options...
garyneedham Posted July 11, 2011 Author Share Posted July 11, 2011 Hi I am still having problems with this search can anyone make this any clearer for me $sql = "SELECT * FROM Customer WHERE Serial LIKE '%" . mysql_escape_string($_GET[serial']) . "%' ORDER BY Serial "; if (empty($_GET['Serial'])) $sql = "SELECT * FROM Serial WHERE 1 = 2 "; $res = mysql_query($sql); ?> I do not really understand the "AND" command and how to use it I want to search 8 columns Serial1 > Serial 8 Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1241176 Share on other sites More sharing options...
AyKay47 Posted July 11, 2011 Share Posted July 11, 2011 basically an AND statement allows you to add more than one condition in addition to a WHERE statement $sql = "SELECT * FROM Customer WHERE Serial LIKE '%" . mysql_escape_string($_GET[serial']) . "%' AND tbl_col LIKE '%something%' ORDER BY Serial "; Also, I don't recommend sanitizing input inside of a query function...can get messy and at times lead to unnecessary errors. Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1241538 Share on other sites More sharing options...
garyneedham Posted July 12, 2011 Author Share Posted July 12, 2011 Hi I have tried the following but the search still does not work can you please check what I have done $sql = "SELECT * FROM Customer WHERE MachineSerial1 LIKE '%" . mysql_escape_string($_GET['Search_Box']) . "%' AND MachineSerial12 LIKE '%" . mysql_escape_string($_GET['Search_Box']) . "%' ORDER BY MachineSerial1 "; if (empty($_GET['Search_Box'])) $sql = "SELECT * FROM MachineSerial1 WHERE 1 = 2 " ; $res = mysql_query($sql); Thanks for your time looking at this Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1241709 Share on other sites More sharing options...
fenway Posted July 14, 2011 Share Posted July 14, 2011 "does not work" isn't useful -- what doesn't work about it? Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1242502 Share on other sites More sharing options...
garyneedham Posted July 15, 2011 Author Share Posted July 15, 2011 Hello Sorry for not explaining myself very clearly. What I mean by not working is "I get no result back and no error." If I use the following statement it works OK but this searches only one field $sql = "SELECT * FROM Customer WHERE MachineSerial1 LIKE '%" . mysql_escape_string($_GET['Search_Box']) . "%' ORDER BY MachineSerial1 "; However if I use the AND command like below it does not look at the fields Machineserial1 and Machineserial1 $sql = "SELECT * FROM Customer WHERE MachineSerial1 LIKE '%" . mysql_escape_string($_GET['Search_Box']) . "%' AND MachineSerial12 LIKE '%" . mysql_escape_string($_GET['Search_Box']) . "%' ORDER BY MachineSerial1 "; What I am looking for is to type a search into the search box called "Search_Box" and bring back a search from eight fields named MachineSerial 1 to 8 I hope I have made this clear Thanks again for any help you can give me Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1242992 Share on other sites More sharing options...
fenway Posted July 15, 2011 Share Posted July 15, 2011 I don't think you mean AND -- otherwise, you're only getting back those rows which contain your search value in BOTH fields. Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1243034 Share on other sites More sharing options...
garyneedham Posted July 21, 2011 Author Share Posted July 21, 2011 Yes I think you are correct But a pointer in the right direction would be nice Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1245559 Share on other sites More sharing options...
TeNDoLLA Posted July 21, 2011 Share Posted July 21, 2011 Use OR instead of AND. Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1245562 Share on other sites More sharing options...
rallport Posted July 21, 2011 Share Posted July 21, 2011 For things like the OPs issue it is always useful to output the contents of the sql query onto the page to debug - as it's hard sometimes to see the generated query directly from code. Just my 2c Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1245884 Share on other sites More sharing options...
garyneedham Posted July 22, 2011 Author Share Posted July 22, 2011 Hi I have managed to get it to work with the following code, not sure if its the best way though. $sql = "SELECT * FROM Customer WHERE MachineSerial1 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial2 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial3 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial4 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial5 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial6 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial7 = '" . mysql_escape_string($_GET['Search_Box']) . "' or MachineSerial8 = '" . mysql_escape_string($_GET['Search_Box']) . "' ORDER BY MachineSerial1 " ; if (empty($_GET['Search_Box'])) $sql = "SELECT * FROM MachineSerial1 WHERE 1 = 2 "; $res = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/241325-how-do-i-search-8-different-fields-columns/#findComment-1246039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.