Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error in query when search term includes apostrophe

Status
Not open for further replies.

skicamel

Programmer
Dec 24, 2001
126
US
Didn't have much luck searching the site for this one, but I'll bet it's in here somewhere.

I've got a very basic php search page that queries an MSSQL database. You throw in a name, it searches. The obvious problem is when you put in a name such as O'Connell, the query hits the apostrophe and proceeds to tell me I'm an idiot for using an apostrophe (not in those exact words).

I'd imagine there's a blatantly simple workaround here that I just can't put my finger on. Thanks for the help, folks.
 
I think you'll need to delimit the apostraphe with a \ so
$query="select * from blah where name = 'O'connel'"
$new_query=addslashes($query);
then run your query.

I may be wrong, I play with mysql not MSSql. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
in MSSQL you must protect the ' with another one.

so, you must do the search with O''Connor

See more information about this in php manual.

See the php manual and search for magic_quotes_sybase. I don't use it, cause i have older programs, but i know that this solves your problem.

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I didn't even know the command...new to PHP. Apparantly, it seems, that it was adding the slashes itself without the command(?). Checked the manual, found some very helpful ideas.

Ended up using:

ereg_replace("'","''",$variable);
$query="select * from blah where name = '$variable'"

So the query sent would be:
select * from blah where name = 'O''Connell'

Don't know about MySql, but MSSQL that's just what was needed.

Thanks, never would have got there without the help.
 
Thanks Anikin...Looks like you posted as I was writing.
 
Anikin to the rescue !! :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
:)

Always when people need, and when i can.

I must defend my second place here ... so i must provide nice tips to keep getting some stars (kidding, i just do it for fun).

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top