0

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

$testvalue="68104";
$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...)

Thanks,
~kyle

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by kyleknapp
0

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

0

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.

0

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

0

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

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

0

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

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.