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

Recommended Answers

All 5 Replies

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.

Thanks,
~kyle

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

~kyle

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.