Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query string using LIKE %$var% 1

Status
Not open for further replies.

craigglenn

Technical User
Oct 30, 2003
42
US
Hi all,

I think this is pretty simple but I am new to PHP.

How do I alter this line to make my search with the wildcard % at the beginning and end of my variable. I am altering some existing code.

if(!empty($searchTerms))
{
$query .= $wpdb->prepare("AND company_name LIKE %s",$searchTerms);
}


Thanks in advance for the help.

Also as a bonus question can anyone tell me what the 's' is for in the %s?

Craig

"You can have anything in life you want if you help enough people get what they want"
Zig Ziggler
 
Code:
if(!empty($searchTerms)) 
    {
        $query .= $wpdb->prepare("AND company_name LIKE %s",'%'.$searchTerms.'%');
    }

the %s tells printf and sprintf to substitute the term for a string. %d for a digit etc. check out the manual on printf() for all the available options.

the alternative is to use regexp rather than like. i doubt that for simple searches there is any advantage.
 
Thank you so much. This worked like a charm!

Plus thanks for the %s lesson!

Craig Glenn

"You can have anything in life you want if you help enough people get what they want"
Zig Ziggler
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top