What is the PHP stripos() equivalent in MySQL?

MySQLMySQLi Database

The stripos() equivalent in MySQL is INSTR(), which returns the position of the first occurrence of a string in another string. Following is the syntax −

select instr(yourColumnName,yourWord) As anyAliasName from yourTableName;

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Title text
   -> );
Query OK, 0 rows affected (1.22 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('MySQL is my favourite subject');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable values('MongoDB is not my favourite subject');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

output

+-------------------------------------+
| Title                               |
+-------------------------------------+
| MySQL is my favourite subject       |
| MongoDB is not my favourite subject |
+-------------------------------------+
2 rows in set (0.00 sec)

Here is the query to get the stripos() equivalent in MySQL returning the position of the first occurrence of the a string −

mysql> select instr(Title,'favourite') As Position from DemoTable;

Output

+----------+
| Position |
+----------+
| 13       |
| 19       |
+----------+
2 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 11:44:34
Advertisements