PDA

View Full Version : PHP, mySQL and Apostrophes



sceneshift
May 22nd, 2008, 05:22 PM
I am trying to setup a search function using the "LIKE %$var%" method, but I have run into a problem.

The data in the database is like so: "Person's Item"

If I search "Person's Item" the data is retrieved successfully.

If I search "Persons Item" without an apostrophe, the retrieval is unsuccessfully.

Is there any way to get mySQL to ignore apostrophes both on the user and database side? so either of the above examples can be entered and successful?

dj_kyron
May 23rd, 2008, 03:10 PM
So the data in the field is ASDFADSF's JKLASDF right? And when you are searching the table, you want to ignore the apostrophe right? It all depends on how you are going to search the table. Are you going to first get all the data and then loop through the results and see if any of the rows qualify? Or are you going to be doing a MySQL full text search?

The MySQL full text search syntax would be something like this:

SELECT * FROM tableOfStuff WHERE keywords LIKE '%$name%'

But with this method I am not totally sure if you can ignore the apostrophes. Worth a shot though.

borrob
May 26th, 2008, 06:19 AM
You can use escape characters in the statament so this should work:

like '%person\'s%'

Mardala
May 26th, 2008, 08:02 PM
there is a function in php for this: addslashes