/    Sign up×
Community /Pin to ProfileBookmark

MySQL LIKE statement problems…

Hi guys,

I’m trying to get data from my database by filtering what is specified. I want to have basically two conditions for my query.
1. Select the type.
2. Type in the value you’re looking for.

Example currently I have this:

[code=php]mysql_query(“SELECT * FROM $db_tbl_assets WHERE pc_type=’$fClass’ AND pc_tag_num=’$fValue'”);[/code]

But I want to have something like this:

[code=php]mysql_query(“SELECT * FROM $db_tbl_assets WHERE pc_type=’$fClass’ AND pc_tag_num LIKE ‘$fValue%'”);[/code]

But it’s not working for some reason (where I have the multiple conditions). Like I want to be able to select all records that, let’s say, have “LTC” in the pc_type column and THEN filter through those by using the pc_tag_num LIKE ‘$fValue%’… understand?

Thanks!!

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 07.2007 — Your statement should work - as long as LTC is at the beginning of the character fields being queried.

If it's somewhere in the middle, you should use: LIKE '%$fValue%'.
Copy linkTweet thisAlerts:
@paradoxperfectauthorFeb 07.2007 — Oops.... I guess I meant to point out that the pc_type can be "LTC" or "DTC" or what ever... but it's not working...
Copy linkTweet thisAlerts:
@NightShift58Feb 07.2007 — It boils down to the same, albeit with another value.

If you want to check against $fValue being anywhere in the field pc_tag_num (beginning, middle or end), use: pc_tag_num LIKE '%$fValue%'.

If only expected (or wanted) at the beginning, use: pc_tag_num LIKE '$fValue%'.

And if only at the end, use: pc_tag_num LIKE '%$fValue'.

Assuming the data is there to support such a search, it should work.
Copy linkTweet thisAlerts:
@paradoxperfectauthorFeb 07.2007 — Ah, I found the problem NightShift... it was because I was padding zeros (up to 4 places) to the numbers... and I guess the query didn't like that when using the LIKE statement. So for example it would always look for '0001%' and not '1%' when I would type in the box... silly me ? Thanks for the help though!
×

Success!

Help @paradoxperfect spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...