0

Is there any other way to perform this far from LIKE and REGEX , I am trying to check if string A contain string B or a little part of string B like example below string A = "United State of America" string B = "America"

String B should be checked if it exist in String A

Currently i have used REGEXP and LIKE but sometimes it give me false output, please check mycodes below and give any suggestion.

//Using REGEXP
$first_five = substr($data[0], -7, 7);
$query = mysql_query("SELECT * FROM 'football' WHERE 'TEAMS' REGEXP '$first_five'");
//Using LIKE
$query = mysql_query("SELECT * FROM 'football' WHERE 'TEAMS' LIKE '%$first_five%'");`

Any idea which query can perform same job like my above querys??????

2
  • 1
    In which cases does it give false output? Please show the exact inputs and what happens Commented Jul 30, 2017 at 14:21
  • I am not that good with mysql but as far as i know if i execute the query the output to display must contain the criteria in my query, so for example if i execute the above query whether with REGEXP or LIKE method the output must contain what i have describe in my condition but you will find output that come they don't even contain what i have described in my condition, Hope you got my point. @SamiKuhmonen Commented Jul 30, 2017 at 15:54

2 Answers 2

2
mysql> SELECT INSTR('foobarbar', 'bar');
+---------------------------+
| INSTR('foobarbar', 'bar') |
+---------------------------+
|                         4 |
+---------------------------+
1 row in set (0.01 sec)

mysql> SELECT INSTR('xbar', 'foobar');
+-------------------------+
| INSTR('xbar', 'foobar') |
+-------------------------+
|                       0 |
+-------------------------+
1 row in set (0.00 sec)
Sign up to request clarification or add additional context in comments.

Comments

0

First of all: Notice about deprecation warning:

mysql_query Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

In second place: You should to escape parameters (be careful, also deprecated)

$sql = "SELECT * FROM football WHERE TEAMS LIKE '%" . 
        mysql_real_escape_string($first_five) . 
        "%'";
$query = mysql_query($sql);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.