Solved

using special characters in a php script

Posted on 2011-09-15
5
222 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
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.

867 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

16 Experts available now in Live!

Get 1:1 Help Now