944,209 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Unsolved
  • Views: 4965
  • MySQL RSS
Oct 13th, 2006
0

variable in WHERE...LIKE construct doesn't work

Expand Post »
why does
MySQL Syntax (Toggle Plain Text)
  1. $sql = 'SELECT userid FROM `phplist_user_user_attribute` WHERE `attributeid`= 7 AND `value` LIKE "68104"';
  2. $query = mysql_query($sql);
find 23 records, but
MySQL Syntax (Toggle Plain Text)
  1. $testvalue="68104";
  2. $sql = 'SELECT userid FROM `phplist_user_user_attribute` WHERE `attributeid`= 7 AND `value` LIKE "$testvalue"';
  3. $query = mysql_query($sql);
finds none? Shouldn't they be the same?

Can I not use a variable as a parameter for LIKE? Or is it a data-type issue (e.g. $testvalue is storing as a number rather than a string?)

Looks to me like these should work the same. I'm baffled (but then that's nothing new...)

Thanks,
~kyle
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyleknapp is offline Offline
15 posts
since Aug 2006
Oct 13th, 2006
0

Re: variable in WHERE...LIKE construct doesn't work

got it figured out. Somehow an escape character was getting inserted in my $testvalue (in real use I was reading values from a file, and the linefeed at the end of each record was getting read into the variable, though why it would happen in the above "hardcoded" example beats me)

I solved the problem by replacing
$testvalue = $zlist[$i];
with
$testvalue = trim($zlist[$i]);

that seemed to fix it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyleknapp is offline Offline
15 posts
since Aug 2006
Oct 13th, 2006
0

Re: variable in WHERE...LIKE construct doesn't work

you dont seem to be using any wild cards for your query, so you probibly dont even need to use like
MySQL Syntax (Toggle Plain Text)
  1. SELECT * FROM table WHERE id LIKE '123%'
also in your example you had your query in double quotes inside of single quotes, this means that you were looking for $testvalue literally and not the variable.
Reputation Points: 10
Solved Threads: 2
Junior Poster
sn4rf3r is offline Offline
133 posts
since Sep 2006
Oct 13th, 2006
0

Re: variable in WHERE...LIKE construct doesn't work

That makes sense to me, but I don't think it's true. If I put the variable in single-quotes the script just crashes. It works well now - it's in double-quotes, but the selection is definitely using the value of the variable, not the literal string.

As for the use of "LIKE", some of the zipcodes in the database are 9-digit, the wildcard is necessary to catch them.

Thanks,
~kyle
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyleknapp is offline Offline
15 posts
since Aug 2006
Oct 13th, 2006
0

Re: variable in WHERE...LIKE construct doesn't work

youre right, I had the two reversed,
single quotes == literal

I didnt see any wildcard in your query, thats why I made that comment.
Reputation Points: 10
Solved Threads: 2
Junior Poster
sn4rf3r is offline Offline
133 posts
since Sep 2006
Oct 13th, 2006
0

Re: variable in WHERE...LIKE construct doesn't work

oops, you're right - I think I added that after my last post...

Thanks for all your help - I have a long ways to go, but at least I'm starting to feel like I have a clue...

~kyle
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kyleknapp is offline Offline
15 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC