how to write Regular Expression in MySQL select queries? - php

I tried this expression /\b(word\w*)\b/i to compare a word against the list of other words to find the duplicates. I used preg_math_all() and it worked fine. I wanted to do the same thing but this time check against the words retrieved from mysql database. This is what i wrote
SELECT * FROM table WHERE word = /\b(word\w*)\b/i
this didnt worked.

Your query should look like:
SELECT * FROM table WHERE column REGEXP '/\b(word\w*)\b/i'

MySQL regex syntax is a different from PHP's, you don't need the delimiters at the start and end.
SELECT * FROM table WHERE word REGEXP '\b(word\w*)\b'
You can't use the i flag for case insensitivity the same way as in PHP, as in MySQL whether the regex is case sensitive or not depends on whether the column collation is a case sensitive one or not.

Related

MYSQL Email Validation with Regex not working

i just want to fetch the invalid email addresses from my database, i tried with the following query, but its not working
$sql=mysql_query("SELECT * FROM mytable WHERE email!='' and email NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$'");
And the invalid Email is a.bcdefg-3#abccom
It looks like the Richard answer is correct however, it may not works given the collation used.
Therefore, if you have a case sensitive collation, you may want to lowercase your field.
Try this query :
SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[[:alnum:]._%-\+]+#[[:alnum:].-]+[.][[:alnum:]]{2,4}$';
I have updated the regex to use character classes instead of character range to avoid lower (or upper) case transformation.
Moreover, in some IDE, you may have to escape "." with two backslashes, therefore I use
[.]
instead of escaped dot.
I updated again to allow subdomains.
Edited to allow +, thanks to #Charlie Brumbaugh comment.
Try this:
SELECT * FROM `mytable` WHERE `email` NOT REGEXP '^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$';
Works for my.
Furthermore, do not use the MySQL driver as it is deprecated in PHP.

How to get all database having upper case character after some word using MYSQL

I want to list all database which has the common starting word and after it should have uppercase letters. (using MYSQL)
eg:
test_vino_JY
test_vino_JI
test_vino_ij
test_vino_klm
In the above example i want to list only test_vino_JY, test_vino_JI
May i know how to do that. I tried using the below query, its not working. Please help me on this.
SHOW DATABASES WHERE `Database` REGEXP '^test_vino_+[A-Z]';
show doesn't accept regexes, it only accepts a show foo like '%...%' wildcard-type matches. You'll have to select against the information_schema pseudo-db:
SELECT *
FROM information_schema.schemata
WHERE SCHEMA_NAME REGEXP '...';

Extract all tables from an SQL query

I want to add a prefix to tables and have recently written a PHP script to extract all tables in a string SQL query.
$sql = 'UPDATE festivals SET desc = "Starts from July"';
preg_match_all('/(from|into|update|table|join) (`?\w+`?)\s/i', $sql, $matches);
It works good but the only problem is that it extracts July because it does not distinguish between a SQL value and a real table name, so it assumes that July would be a table too.
Now I think the solution should be something to prevent extract what wrapped in a single or double quotation but don't know how to do that.
Your regex is better off this way:
"/((?:^select .+?(?:from|into))|^update|^table|join) (`?\w+`?)\s/I"
But I still agree with nvartolomei.
If you were more strict in your query-writing, you would wrap all your database, table and column names in backticks ` and they would be extremely easy to extract - just get the first match of a string between them: just make the backticks required instead of optional.
That said, I'm not entirely sure how your regex is matching from July since the July is not followed by whitespace...

MySQL: escape space from query

Is there a way using MySQL, to query a value containing a space, but as some sort of escape character? It's for an instant search engine I'm building (I'm trying to incorporate special search strings such as quotes to search for exact string).
cheers
select * from sometable where somefield like '%oh no%';
Full-text indexes are what you should use. They handle all sorts of key words/characters (such as quoting a string to get exact match, -someword for saying someword can't be in the result and +someword for saying someword has to be in the result) without having to do anything special in your code (other than changing your query a little). The database will do the search for you and return the most relevant results at the top of the query. It is really quite easy to get going too.
Mysql Manual
Using full-text searching (implementation help)

match a word from a sentence in MYSQL using regex

How can I match a word in a sentence from a column using regex? I tried:
"select * from table where column regex '/\b".$text."\b/i'"
but that didn't work.
Try this:
"SELECT * FROM table WHERE column REGEXP '[[:<:]]" . $text . "[[:>:]]'"
You should make sure that any characters that could be interpreted as special characters in $text are properly escaped. You should also ensure that you do not get an SQL injection.
MySQL is particularly bad about stuff like this. I'd recommend using MATCH AGAINST syntax http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html or using something like SOLR if you're going to be doing this in volume.
I think you want the LIKE clause!
SELECT * FROM table WHERE column LIKE '%value%'
Also please read UltimateBrent's answer as full text search is a very good point.

Categories