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

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.

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.

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

Archived

This topic is now archived and is closed to further replies.

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