Wayne
Wayne

Reputation: 343

Using REGEX in MySQL

Within my MySQL table I have a row with this value for a column:

^[\/]?(Home|Index)\.php$

I would like to use this regex value during lookup rather than within my script.. I've looked around and found that MySQL IS capable of performing regex based queries, however I've only found resources which have the regex value within the query, not within table values..

I will admit that I could, very well, just be searching for the wrong information while I'm using search engines but I don't really know how to word this in a way that a search engine would understand..

I would greatly appreciate any information that could be supplied to me, even if that's only so much as a correct term to search for on Google..

Edit: Table structure:

CREATE TABLE IF NOT EXISTS `pages` (
`uid` int(12) NOT NULL,
  `title` varchar(70) NOT NULL,
  `href` text NOT NULL
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2;

Single row of content:

INSERT INTO `pages` (`uid`, `title`, `href`) VALUES
(1, 'Home', '^[\\/]?(Home|Index)\\.htm[l]?$');

I made one attempt at actually obtaining a result, probably screwed up, but...

SELECT * FROM `pages` WHERE REGEXP `href`, '$_SERVER[REQUEST_URI]'

Although it's likely clear at this point, my processor is php.

Upvotes: 0

Views: 58

Answers (1)

chris85
chris85

Reputation: 23892

Your regexp expression syntax is incorrect. It should be

SELECT * FROM `pages` WHERE '$_SERVER[REQUEST_URI]' REGEXP `href`

Value REGEXP (as a keyword) then the expression.

https://dev.mysql.com/doc/refman/5.1/en/regexp.html

Upvotes: 1

Related Questions