
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP basename Equivalent in MySQL
If given a string containing a path to a file, the PHP basename() function will return the base name of the file. To get its equivalent in MySQL, you can use SUBSTRING_INDEX(). Let us first create a table −
mysql> create table DemoTable -> ( -> Location varchar(200) -> ); Query OK, 0 rows affected (1.02 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('C:\Web\Sum.java'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('E:\WebDevelopment\Image1.png'); Query OK, 1 row affected (0.42 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+------------------------------+ | Location | +------------------------------+ | C:\Web\Sum.java | | E:\WebDevelopment\Image1.png | +------------------------------+ 2 rows in set (0.00 sec)
Following is the query to work with the basename() equivalent in MySQL and get what the basename() function returns i.e. the base name of the file −
mysql> select Location, -> SUBSTRING_INDEX(Location,'\', -1) AS NameOfFile from DemoTable;
Output
+------------------------------+------------+ | Location | NameOfFile | +------------------------------+------------+ | C:\Web\Sum.java | Sum.java | | E:\WebDevelopment\Image1.png | Image1.png | +------------------------------+------------+ 2 rows in set (0.00 sec)
Advertisements