why does

$sql = 'SELECT userid FROM `phplist_user_user_attribute` WHERE `attributeid`= 7 AND `value` LIKE "68104"';
$query = mysql_query($sql);

find 23 records, but

$sql = 'SELECT userid FROM `phplist_user_user_attribute` WHERE `attributeid`= 7 AND `value` LIKE "$testvalue"';
$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...)


11 Years
Discussion Span
Last Post by kyleknapp

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


you dont seem to be using any wild cards for your query, so you probibly dont even need to use like

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.


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.



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

I didnt see any wildcard in your query, thats why I made that comment.


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...


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.