lovephp Posted August 31, 2013 Share Posted August 31, 2013 friends in my db table say like customerName holds First and the Last name but in my search if i enter the first name then record shows but if i try to search by entering both First and the Last name in the textbox i do not get any results why? here the query $srcquery = $_REQUEST['srcquery']; $query = "SELECT COUNT(*) as num FROM $tableName WHERE customerName LIKE '%".$srcquery."%' OR homePhone LIKE '%".$srcquery."%' OR comments LIKE '%".$srcquery."%' AND agent = '".$logged."' ORDER BY id DESC"; Quote Link to comment Share on other sites More sharing options...
.josh Posted August 31, 2013 Share Posted August 31, 2013 not a sql expert but here's one way to do it... $srcquery = str_replace(" ","|",$_REQUEST['srcquery']); $query = "SELECT COUNT(*) as num FROM $tableName WHERE customerName REGEXP '".$srcquery."' OR homePhone REGEXP '".$srcquery."' OR comments REGEXP '".$srcquery."' AND agent = '".$logged."' ORDER BY id DESC"; one thing to note here though is you really should look into sanitizing user input before using it in a query. Google sql injection. Quote Link to comment Share on other sites More sharing options...
lovephp Posted August 31, 2013 Author Share Posted August 31, 2013 or say this query SELECT * FROM $tableName WHERE customerName LIKE '%".$srcquery."%' OR homePhone LIKE '%".$srcquery."%' OR comments LIKE '%".$srcquery."%' ORDER BY id DESC i enter peter i get result i enter peter smith nothing show i enter smith then result shows again. Quote Link to comment Share on other sites More sharing options...
lovephp Posted August 31, 2013 Author Share Posted August 31, 2013 not a sql expert but here's one way to do it... $srcquery = str_replace(" ","|",$_REQUEST['srcquery']); $query = "SELECT COUNT(*) as num FROM $tableName WHERE customerName REGEXP '".$srcquery."' OR homePhone REGEXP "'.$srcquery."' OR comments REGEXP '".$srcquery."' AND agent = '".$logged."' ORDER BY id DESC"; one thing to note here though is you really should look into sanitizing user input before using it in a query. Google sql injection. thanks bro and yes i do use escape strings but just to show here i removed it all to make it simple. ill give it a try with what you gave me hope it sorts my problem. i have never made a search script before so im clueless Quote Link to comment Share on other sites More sharing options...
.josh Posted August 31, 2013 Share Posted August 31, 2013 check my posted code again, the one you quoted in your post has one of the single/double quotes wrong Quote Link to comment Share on other sites More sharing options...
lovephp Posted September 1, 2013 Author Share Posted September 1, 2013 it did not work out brother, no result shows. any help? Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 1, 2013 Share Posted September 1, 2013 What is the EXACT value of the name field in the database that you think "peter smith" should match? Quote Link to comment 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.