using special characters in a php script

I need to write a query in a php script that using LIKE what do I need to add to specail chars such as % for it to work?
$query ="SELECT * FROM table WHERE field LIKE ' ". %$name % . ' " ;

Thank you.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Lukasz ChmielewskiConnect With a Mentor Commented:
Try to use double %% sign
Lukasz ChmielewskiCommented:
$query ="SELECT * FROM table WHERE field LIKE '%% ".$name."'%%" ;
Ray PaseurCommented:
Not sure I am reading the question the same way as Roads_Roads.

If you want to use wild-card matching, you use the LIKE and you use a string with a percent sign to match wild card strings.

Note that % matches any number of characters, even zero characters.  Underscore matches one character.

To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the ESCAPE character, “\” is assumed.

HTH, ~Ray
if you want to search these special characters [%_]  use slash to include them literally
Mostafa_BZConnect With a Mentor Commented:
f you want to search these special characters without any problem use the following:
$search_word = '%text%';
$search_word = addslashes($search_word);
$search_word = htmlentities($search_word,ENT_NOQUOTES,'utf-8');
$search_word = "%".$search_word."%";
$query ="SELECT * FROM table WHERE field LIKE '".$search_word."'";

Open in new window

All Courses

From novice to tech pro — start learning today.