Building a SQL query with LIKE and 's

Do you have questions regarding other database enginges (not MySQL) -- ask here!

Moderators: macek, egami, gesf

Post Reply
User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Building a SQL query with LIKE and 's

Post by egami » Wed Oct 26, 2011 4:40 am

SELECT * FROM table WHERE field LIKE '%s%'

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Building a SQL query with LIKE and 's

Post by Nullsig » Wed Oct 26, 2011 9:43 am

Have you tried echo'ing out the dynamically produced query and executing it on the SQL server (through some method other than utilizing your PHP server)

Sometimes actually reading through the query once it is completely generated helps ascertain what is incorrect about it. From your description (and no knowledge about the content or structure of your database) your query looks correct and the type of slashes should not be causing the problem.

All of this is assuming that you are, as you say, not a complete idiot.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Building a SQL query with LIKE and 's

Post by egami » Wed Oct 26, 2011 11:57 am

After smoking a huge blunt, and taking a couple of vicadine, I think I know what the issue is.

IF your variable that you're doing the search on as a tick or a back-tick in the variable, that will end the "string" to search for, but that leaves one open tick left, and then the script bombs.

$variable = addslashes($variable);

before your query and see if that makes the difference.

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Building a SQL query with LIKE and 's

Post by Nullsig » Thu Oct 27, 2011 7:49 am

So just to clarify...

The query shows up as:

Code: Select all

SELECT * FROM dbname WHERE field LIKE \'a%\'
When you use addslashes OR does it happen when you don't use addslashes.

If the latter is the case it seems like your server utilizes magic quotes or something of the sort. In which case you would need to use "stripslashes()" and it should work.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest