Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

using special characters in a php script

Posted on 2011-09-15
5
Medium Priority
?
262 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 1000 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 111

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 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

971 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