954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP Error in searching (Help!)

Hello PHP experts how can you detect ' character in your search field?
Because every time I input the word hilgard's it returns an error I think because of the character '. This is the error

Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Title LIKE '%hilgard's%';'., SQL state 37000 in SQLExecDirect in D:\wamp\www\Online Library\opac.php on line 76
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Title LIKE '%hilgard's%';'.

tnx in advance.

werks
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

What happens is, the query ends when it encounters ' in hilgard's. Escape it with \.
Eg.

'SELECT *
FROM `table3`
WHERE name LIKE '%hilgard\'s%'

To escape the single quote, you can use addslashes (or mysql_real_escape_string if your database is mysql). Or
You can use double quotes.

"SELECT *
FROM `table3`
WHERE name LIKE '%hilgard's%"


:) Cheers,
Naveen

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Hello nav33n i've tried your code

"SELECT *
FROM `table3`
WHERE name LIKE '%hilgard's%"


but still it returns an error

werks
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

hmm.. weird.. Did you try the other option ? escaping ' ?

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
"SELECT *
FROM `table3`
WHERE name LIKE '%hilgard\'s%'"
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 
"SELECT *
FROM `table3`
WHERE name LIKE '%hilgard\'s%"

Oh right ! I think I need to sleep for some more time! ;)

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

Solved it. I use $exp = str_replace("'", "", $exp); BTW Thanks to the two of you..

werks
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You