match a word from a sentence in MYSQL using regex - php

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.

Related

Escaping meta characters in PHP

I was surprised to discover that the MySQL query
SELECT * WHERE name LIKE "%AFA_";
returns rows where name is SAFARI. To get it to match on the underscore, you have to do:
SELECT * WHERE name LIKE "%AFA\_";
Is there a PHP function that can do this transition or do I have to use str_replace?
PHP has no knowledge of MySQL LIKE wildcards, nor should it.
It does, however, have a way to escape things in strings if you want, and that is str_replace.
Replace instances of _ with \_, or whatever you like.
Ultimately this question has nothing to do with MySQL.

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 query: return rows where field is made of characters from the given php string

I am looking for a mysql query which will return me all the rows,
where a particular field is made up of characters from a given string.
e.g. I 've this string: $input = 'abcdef';
now, I am looking for mysql to result all those rows, where e.g. 'name' consists of letters from $input.
I hope I have expressed myself clearly.. :)
EDIT: Also, for the query you suggest, will it really improve performance, if we are just searching for strings made of letters a-z instead of some patterns, esp. when we are requesting data every 2 seconds.
The thing is that for now, my requirements are to only search among numeric, alpha or alphanumeric formats.. I will be using the exclusive patterns later on.
Regards
Nikhil Gupta
$query = "SELECT * FROM table WHERE `name` REGEXP '[" . mysql_real_escape_string($input) . "]'";
But this query is really weird and will be very slow (depends on how many rows you have).
SELECT `name` FROM `table` WHERE `name` REGEXP '^[abcdef]*$'
See MySQL: Regular expressions.

how to write Regular Expression in MySQL select queries?

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.

Categories