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!

MySQL query problem

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi

Why is it when I perform a command in PHP such as below it fails (i.e. no records are returned):
SELECT * FROM my_table WHERE field_name='Here\'s a test'

I have the situation where the field contents may include chars such as ' or "

I am ensuring addslashes is performed before writing the data to the database (I currently have Magic Quotes turned off), and in this case at least one record has this field set as "Here\'s a test"

Surely this isn't a limitation of the WHERE clause?


Regards

Andrew Blee.
 
try
WHERE field_name='Here''s a test'
instead

hope this helps

 
Hi

Thanks for replying.

Trying what you suggested worked if within the database I stored "Here's a test", but then from what I understand this isn't the correct way of doing things, all chars such as ' should have a \ before it within the database, although I think this is because when sending a query string in PHP to MySQL, chars such as ' will cause problems if not slashed?

So it seems the ' char stops a MySQL query from failing if the ' char is to be included in the search string, this is good, but is it possible to get this working if within the database "Here\'s a test" is stored.

Perhaps the answer is to replace the ' with two of these before storing them into the database, and then your suggestion would work. I'm not sure if the same applies for the " char though.

Any other thoughts appreciated.

Andrew Blee.

 
well i just created a test table, added there "Here's a test" and tried to get it via php
everything is ok

i used
@$result = mysql_query("select * from testtbl where stringcol = 'Here''s a test'");

look in the mysql documentation - there is somewhere about escaping such characters like "'"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top