mark107 Posted January 18, 2020 Share Posted January 18, 2020 (edited) Hi all, I am working on the PHP to fetch the data from the database. I would like to fetch the filename only following by: email1.png email2.png email3.png my_inbox.png When I try this: $mailbox_sql = 'SELECT * FROM ' . $mailfolder . ' WHERE email_id = ? AND message_id = ?'; $mailbox = $link->prepare($mailbox_sql); $mailbox->execute([$id,$message_id]); // set the resulting array to associative $row = $mailbox->fetch(PDO::FETCH_ASSOC); if (is_array($row)) { $attached = $row['attached_files']; $attached_files_name = explode('attid: ', $attached); } I am getting this: 0 filename: email1.png 1 filename: email2.png 2 filename: email3.png 3 filename: my_inbox.png Here is what I have stored in the database: attid: 0 filename: email1.png attid: 1 filename: email2.png attid: 2 filename: email3.png attid: 3 filename: my_inbox.png Here is what I want to achieve: email1.png email2.png email3.png my_inbox.png Can you please show me example how I could get the filename when I am fetching the data from the database? Thanks in advance. Edited January 18, 2020 by mark107 Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/ Share on other sites More sharing options...
Barand Posted January 18, 2020 Share Posted January 18, 2020 try SELECT TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename FROM ... Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573622 Share on other sites More sharing options...
mark107 Posted January 18, 2020 Author Share Posted January 18, 2020 (edited) 6 minutes ago, Barand said: try SELECT TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename FROM ... Why do I need to do that as I have already fetching the data and store in the array? I want to split the string for value like "0 filename: ", "1 filename: ", "2 filename: ", "3 filename: " or whatever it is to replace with empty string. Edited January 18, 2020 by mark107 Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573623 Share on other sites More sharing options...
Barand Posted January 18, 2020 Share Posted January 18, 2020 2 minutes ago, mark107 said: Why do I need to do that Because that is what you asked for ... 47 minutes ago, mark107 said: Here is what I have stored in the database: attid: 0 filename: email1.png attid: 1 filename: email2.png attid: 2 filename: email3.png attid: 3 filename: my_inbox.png Here is what I want to achieve: email1.png email2.png email3.png my_inbox.png Can you please show me example how I could get the filename when I am fetching the data from the database? Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573624 Share on other sites More sharing options...
mark107 Posted January 18, 2020 Author Share Posted January 18, 2020 4 minutes ago, Barand said: Because that is what you asked for ... It doesn't make sense. I have asked how to remove the string and get the filename. I have tried this and it doesn't work. foreach ($attached_files_name as $filename) { if(strpos($filename, 'attid: ' . $attach_id) !== false) { $filename = str_replace('attid: ' . $attach_id, '', $filename); } if(strpos($filename, ' attid: ' . $attach_id . ' ') !== false) { $filename = str_replace(' attid: ' . $attach_id . ' ', '', $filename); } echo $filename; echo "<Br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573625 Share on other sites More sharing options...
Barand Posted January 19, 2020 Share Posted January 19, 2020 Get the query to do the work. mysql> SELECT attached_files -> , TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename -> FROM mark107; +---------------------------------+--------------+ | attached_files | filename | +---------------------------------+--------------+ | attid: 0 filename: email1.png | email1.png | | attid: 1 filename: email2.png | email2.png | | attid: 2 filename: email3.png | email3.png | | attid: 3 filename: my_inbox.png | my_inbox.png | +---------------------------------+--------------+ Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573626 Share on other sites More sharing options...
mark107 Posted January 19, 2020 Author Share Posted January 19, 2020 6 minutes ago, Barand said: Get the query to do the work. mysql> SELECT attached_files -> , TRIM(SUBSTRING_INDEX(attached_files, ':', -1)) as filename -> FROM mark107; +---------------------------------+--------------+ | attached_files | filename | +---------------------------------+--------------+ | attid: 0 filename: email1.png | email1.png | | attid: 1 filename: email2.png | email2.png | | attid: 2 filename: email3.png | email3.png | | attid: 3 filename: my_inbox.png | my_inbox.png | +---------------------------------+--------------+ I dont store these strings seperate. I stored altogether. See this for yourself ! Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573627 Share on other sites More sharing options...
mark107 Posted January 19, 2020 Author Share Posted January 19, 2020 ??????????????????????????????? Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573629 Share on other sites More sharing options...
Barand Posted January 19, 2020 Share Posted January 19, 2020 9 hours ago, mark107 said: I dont store these strings seperate. I stored altogether. Then you deserve to have problems Try $attached_files = <<<DATA attid: 0 filename: email1.png attid: 1 filename: email2.png attid: 2 filename: email3.png attid: 3 filename: my_inbox.png DATA; $afArray = explode("\n", "$attached_files"); foreach ($afArray as $af) { echo trim(strrchr($af, ':'), ': ') . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573639 Share on other sites More sharing options...
mark107 Posted January 19, 2020 Author Share Posted January 19, 2020 5 hours ago, Barand said: Then you deserve to have problems Try $attached_files = <<<DATA attid: 0 filename: email1.png attid: 1 filename: email2.png attid: 2 filename: email3.png attid: 3 filename: my_inbox.png DATA; $afArray = explode("\n", "$attached_files"); foreach ($afArray as $af) { echo trim(strrchr($af, ':'), ': ') . '<br>'; } Thank you very much for this as it is working great. Problem are now resolved. Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573656 Share on other sites More sharing options...
gw1500se Posted January 19, 2020 Share Posted January 19, 2020 Why in the world would you store it that way in the first place? Thought it may work now, down the road who knows what kind of problems this will present. Now is the time to fix the schema so it makes sense. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309892-how-to-fetch-the-filename-on-split/#findComment-1573657 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.