Solved

using special characters in a php script

Posted on 2011-09-15
5
209 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:Nura111
5 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 250 total points
ID: 36545875
Try to use double %% sign
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 36545883
$query ="SELECT * FROM table WHERE field LIKE '%% ".$name."'%%" ;
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36552229
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.
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

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
0
 
LVL 2

Expert Comment

by:mansooralia_yahoo
ID: 36553871
if you want to search these special characters [%_]  use slash to include them literally
0
 

Assisted Solution

by:Mostafa_BZ
Mostafa_BZ earned 250 total points
ID: 36556940
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

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now