Jump to content

help please, unable to search 2 words together


lovephp

Recommended Posts

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";
 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

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.