How do I query a database WHERE a string has a ' in it?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
3dron
Forum Commoner
Posts: 40
Joined: Sat Feb 01, 2003 10:25 pm

How do I query a database WHERE a string has a ' in it?

Post by 3dron »

I am comparing some titles against a database to get their ID number.

It works fine for all except if the sting has an apostrophe.

Is there an escape character I can ereg_replace into the string?

RR
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

Rather than use regex patterns, try the PHP functions addslashes() or mysql_escape_string()
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

I found it also works if you give it an exta '. For instance if the title is:

Who's that girl?

Then you could just have it add an extra ' changing it to:

Who''s that girl?

And then it gets it just fine....just a thought!!!


Jade
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

addslashes() or mysql_escape_string() - and stripslash() on the way out.

On the way in to a db, I also like to htmlspecialchars() anything that will later be displayed in a browser or form field. Blocks all kinds of nastiness.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

Lol,

your reply makes me sound stupid!!! Oh well, taught me something new as well...can you tell i'm still pretty new at this?? Hehe, and i've been at it for over a year now. Go figures...

Jade
:lol:
Post Reply