corsasport.co.uk
 

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


New Topic

New Poll
  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 RegEx
ed
Member

Registered: 10th Sep 03
User status: Offline
11th Aug 10 at 17:55   View User's Profile U2U Member Reply With Quote

Not done this before, but I want to get videos, audio or images from my database using regex from a mime column. The data in the column would be something like video/quicktime so I would think the regex for that would be video/(.*) to get all videos?

So, why doesn't this work?

SELECT * FROM media_attachments WHERE mime REGEXP '/video/.*/'

Anyone know? It keeps on returning 0 rows, even though there is a video/quicktime row in there.
Dom
Member

Registered: 13th Sep 03
User status: Offline
11th Aug 10 at 18:40   View User's Profile U2U Member Reply With Quote

couldn't you use LIKE?
'^video' will match anything with video in it. If you want to match multiple strings then use '^(video|audio)' (you get the idea)

[Edited on 11-08-2010 by Dom]
ed
Member

Registered: 10th Sep 03
User status: Offline
11th Aug 10 at 19:24   View User's Profile U2U Member Reply With Quote

Sorted it with RegEx. Forgot that a / doesn't mean anything without at \ in front of it. So the query is:

SELECT * FROM media_attachments WHERE mime REGEXP '/video(.*)/'
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
11th Aug 10 at 23:59   View Garage View User's Profile U2U Member Reply With Quote

I think LIKE is less expensive?
Dom
Member

Registered: 13th Sep 03
User status: Offline
12th Aug 10 at 00:19   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Ian
I think LIKE is less expensive?


LIKE is usually around 5/6 times quicker (from what i've experienced in the pasted using JMeter) than REGXP matching similar patterns. Unless you need to do funky pattern matching then i'd stick with LIKE.
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
12th Aug 10 at 00:53   View Garage View User's Profile U2U Member Reply With Quote

Although if you're bothered about speed I'm sure you could normalise the MIME string and query from the junction table. That would be even quicker as you're not doing substring matches anywhere then.

Should join nice as well because you're using foreign keys which are therefore primary in the other tables and therefore indexed.
ed
Member

Registered: 10th Sep 03
User status: Offline
12th Aug 10 at 09:57   View User's Profile U2U Member Reply With Quote

I guess what I could do is only add the content part of the mime. I'm actually not interested in the type after the file has been encoded because it can only be an mp4, ogv or webm if it's a video or an mp3 or ogg if it's audio. No RegEx or like at all then
ed
Member

Registered: 10th Sep 03
User status: Offline
12th Aug 10 at 09:58   View User's Profile U2U Member Reply With Quote

But I think I'll use like for now seeing as it seems more suited
Dom
Member

Registered: 13th Sep 03
User status: Offline
12th Aug 10 at 10:43   View User's Profile U2U Member Reply With Quote

Slightly off topic, you server siding the encoding of files? How you going about that?
ed
Member

Registered: 10th Sep 03
User status: Offline
12th Aug 10 at 12:43   View User's Profile U2U Member Reply With Quote

I've got FFMPEG installed server side and I'm running Linux commands from a php script that gets file info from the MySQL database. Have problems with permissions at the moment though and can't seem to use chmod in php. I think I can run cron jobs as root though which might work...
Dom
Member

Registered: 13th Sep 03
User status: Offline
12th Aug 10 at 21:47   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by ed
I've got FFMPEG installed server side and I'm running Linux commands from a php script that gets file info from the MySQL database. Have problems with permissions at the moment though and can't seem to use chmod in php. I think I can run cron jobs as root though which might work...


hate dealing with permissions and PHP under linux, complete ballache most of the time. Although it's usually because the PHP script doesn't have the right permissions, so it might be worth temporarily chmod 777/rwxrwxrwx the php script.

By the way, you seen the PHPVideoToolkit? It's basically a wrapper/class for ffmpeg and FLVTools. It's what i've used in the past for php video encoding stuff and it works a treat - http://phpvideotoolkit.sourceforge.net/index.html
ed
Member

Registered: 10th Sep 03
User status: Offline
12th Aug 10 at 22:26   View User's Profile U2U Member Reply With Quote

Not seen that. The only related thing I've seen is ffmpeg-php which doesn't do anything re endcoding, will have to take a look...

It seems that ffmpeg will only encode if you've got 0777 set on the file and directory. Easy enough to achieve if I could get the chmod function to work in php. Keeps throwing an error even though the php user is the owner of the file. I guess I know what my Sunday is going to involve

 
New Topic

New Poll

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

Corsa Sport » Message Board » Off Day » Geek Day » MySQL RegEx 29 database queries in 0.0170679 seconds