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

Reply

Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

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

 
0
  #1
Oct 13th, 2006
why does
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

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

 
0
  #2
Oct 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

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

 
0
  #3
Oct 13th, 2006
you dont seem to be using any wild cards for your query, so you probibly dont even need to use like
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

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

 
0
  #4
Oct 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

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

 
0
  #5
Oct 13th, 2006
youre right, I had the two reversed,
single quotes == literal

I didnt see any wildcard in your query, thats why I made that comment.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 15
Reputation: kyleknapp is an unknown quantity at this point 
Solved Threads: 0
kyleknapp kyleknapp is offline Offline
Newbie Poster

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

 
0
  #6
Oct 13th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC