corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » MySQL LIKE


New Topic

New Poll
  <<  1    2  >> Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author MySQL LIKE
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 08:22   View Garage View User's Profile U2U Member Reply With Quote

Iv rewritten the showroom search on vagweb so it now search on image tags. These are seperated in the DB by commas or spaces.

This works fine using the LIKE % $query % statement. So for example if my image has the tags silver, skoda, fabia, vrs then searching for silver skoda, or fabia vrs brings it up fine. But if i search for two tags that arent next to each other in line, ie silver vrs, then it doesnt find it.

Is there any query i can use that brings up any instances of the words anywhere in the line?
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
22nd Oct 07 at 08:27   View User's Profile U2U Member Reply With Quote

A dirty way of doing it would be to split the search string at code level and then pass in say 6 different parameters to the query, you would have to guess how many words the longest search would be though.
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
22nd Oct 07 at 08:40   View User's Profile U2U Member Reply With Quote

You can count the number of words entered (received by the search page) and build the query around the number of arguments, not sure how accurate the search results would be though....
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 14:51   View Garage View User's Profile U2U Member Reply With Quote

hmmz
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
22nd Oct 07 at 15:15   View Garage View User's Profile U2U Member Reply With Quote

Nothing wrong with splitting the queries and a load of OR WHERE, apart from perhaps a bit of speed.

FULLTEXT is also an option.
Laney
Member

Registered: 6th May 03
Location: Leeds
User status: Offline
22nd Oct 07 at 15:59   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Ian
Nothing wrong with splitting the queries and a load of OR WHERE, apart from perhaps a bit of speed.

FULLTEXT is also an option.


Replaces spaces in the searchphrase with %'s?

FULLTEXT is a bit fiddley is it not? Especially when he'd have to change the INI settings to get past the min 4chars rule. Definately a slicker option though
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
22nd Oct 07 at 16:01   View Garage View User's Profile U2U Member Reply With Quote

Replacing space with % wouldn't fix the ordering issue of each of the search queries.
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 16:01   View Garage View User's Profile U2U Member Reply With Quote

came up with this

code:

$words = split(" ", $s);
$count = count(explode(" ", $s));
$c='0';
while ($c < $count)
{
$or = $or . "OR " . $where . " LIKE '%" . $words[$c] . "%' ";
$c++;
}

$sql = "SELECT p.pic_id, p.pic_title, p.pic_desc, p.pic_user_id, p.pic_username, p.pic_time, p.pic_cat_id, p.pic_approval, p.pic_file_name, p.pic_thumbnail, c.cat_id, c.cat_title, c.cat_user_id
FROM " . ALBUM_TABLE . ' AS p,' . ALBUM_CAT_TABLE . " AS c
WHERE p.pic_approval = 1
AND " . $where . " LIKE '%" . $s . "%'
$or
AND p.pic_cat_id = c.cat_id
AND c.cat_view_level <> '3'
ORDER BY p.pic_time DESC";


it works, but can anyone see anything thats likely to cause things to grind to a halt there with a large query?
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
22nd Oct 07 at 16:04   View User's Profile U2U Member Reply With Quote

That's pretty much what I was getting at.

How big is the query that's breaking it?

Also, what is $where?
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 16:05   View Garage View User's Profile U2U Member Reply With Quote

$where is just something that changes when you select the search query type, at the moment the options are, Username, Image Title, and Image Tags
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
22nd Oct 07 at 16:06   View User's Profile U2U Member Reply With Quote

Is there a limit on the length of a PHP string? I'm not hot on PHP. C# FTW

Can you trace out the SQL thats crashing and then run it directly in MySQL to see if its a DB or Code issue?

Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 16:08   View Garage View User's Profile U2U Member Reply With Quote

quote:
Originally posted by James
Is there a limit on the length of a PHP string? I'm not hot on PHP. C# FTW

Can you trace out the SQL thats crashing and then run it directly in MySQL to see if its a DB or Code issue?




its not crashing, it works fine i just wondered if any eagle eyed bodies could forsee that becoming slow with more requests
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
22nd Oct 07 at 16:10   View User's Profile U2U Member Reply With Quote

Not as long as your as DB is designed well. Realistically no-one is going to input a huge string anyway apart from muppetsport

P.s. Thanks for a snippet of your DB structure for SQL injection xxx
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 16:19   View Garage View User's Profile U2U Member Reply With Quote

no worries, shame you dont know my table names

iv got another problem, now i have that list of OR's being created its ignoring my ANDS

any ideas?
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
22nd Oct 07 at 16:23   View User's Profile U2U Member Reply With Quote

Put all the ORs in brackets and use an AND for the group. Like this:

Criteria1 = 1 AND Criteria2 = 2 AND (Criteria3 = 3 OR Criteria4 = 4)

[Edited on 22-10-2007 by James]
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 16:26   View Garage View User's Profile U2U Member Reply With Quote

yeah just figured that out lolz
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
22nd Oct 07 at 17:54   View Garage View User's Profile U2U Member Reply With Quote

I would imagine it is slow just simply because of all the ORs. There's no real way around that as you can't index your way out of a substring.
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
22nd Oct 07 at 18:06   View Garage View User's Profile U2U Member Reply With Quote

nah its not slow at the moment, but iv only tried a few simply queries, just wondering if you could see it being slow in the future?
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
23rd Oct 07 at 03:00   View Garage View User's Profile U2U Member Reply With Quote

More data and more words in your query, yes.
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
23rd Oct 07 at 06:01   View Garage View User's Profile U2U Member Reply With Quote

best way to avoid> limit the query length?
AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
23rd Oct 07 at 07:40   View User's Profile U2U Member Reply With Quote

I'd just limit it to, say, 5 words. But don't tell the users that it only searches for 5 words......just drop any extra words from the search.
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
23rd Oct 07 at 08:38   View Garage View User's Profile U2U Member Reply With Quote

yak theres still issues.

my problem now is if i search for

green seat arosa

it will pull up everything with green | seat | arosa in the tags.

so it pulls up all the ibizas, and leons etc, i want it to only get the green arosas

fuck

[Edited on 23-10-2007 by Steve]
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
23rd Oct 07 at 08:49   View Garage View User's Profile U2U Member Reply With Quote

i need it to get all the queries in the search string, but that can be anywhere in the string
Steve
Premium Member

Avatar

Registered: 30th Mar 02
Location: Worcestershire Drives: Defender
User status: Offline
23rd Oct 07 at 09:31   View Garage View User's Profile U2U Member Reply With Quote

no worries its sorted
James
Member

Registered: 1st Jun 02
Location: Surrey
User status: Offline
23rd Oct 07 at 09:38   View User's Profile U2U Member Reply With Quote

Your ORs needed to be ANDs?

  <<  1    2  >>
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Linking .jsp pages to a MySQL database chris_lee100 Geek Day 7 1435
1st Feb 04 at 14:23
by Ian
 
forum hacking - SQL's Drew Geek Day 15 1720
9th Sep 04 at 23:50
by Ian
 
Server Help Jodi_the_g Geek Day 7 1458
19th Jan 06 at 15:18
by Jodi_the_g
 
My SQL-Nearly there! Tom J Geek Day 31 3189
25th Dec 06 at 23:32
by Ian
 

Corsa Sport » Message Board » Off Day » Geek Day » MySQL LIKE 28 database queries in 0.0137298 seconds