Reputation: 408
I am working on my PHP to fetch the data from the mysql database. I have got a problem with storing the filename in the array using the values when I have stored both filenames in a database.
Here is what I have stored in the blob:
draft_attachment.rar
draft_attachment - Copy.rar
Here is the code:
$mailbox = $link->prepare("SELECT * FROM draft WHERE id = ?");
$mailbox->execute([$id]);
// set the resulting array to associative
$row = $mailbox->fetch(PDO::FETCH_ASSOC);
$attachments = array();
if($mailbox->rowCount() == 1)
{
$attachment = array();
$draft_to = htmlspecialchars($row['to_email']);
$draft_subject = quoted_printable_decode($row['subject']);
$draft_message = $row['message'];
$attach[] = $row['attachments'];
}
?>
When I try this:
$attach[] = $row['attachments'];
It will store both strings in the array:
Array
{
[0] => draft_attachment.rar
draft_attachment - Copy.rar
}
So I have tried this:
$attachments = array();
$i = 0;
if($mailbox->rowCount() == 1) {
$attachments[$i] = $row['attachments'];
$i++;
}
It wont store the strings separate so I dont know what to do.
Here is what I want to achieve:
Array
{
[0] => draft_attachment.rar
[1] => draft_attachment - Copy.rar
}
Can you please show me an example how I could get both strings to go separate so I could be able to store them in the array using the value?
Thank you.
Upvotes: 0
Views: 78
Reputation: 26
you can use explode function, something like explode("\n", $row['attachments'])
Upvotes: 0
Reputation: 147146
You can split the string using preg_split
on \R
(any sequence of new line characters) (explode
can also work if you know the exact line ending characters):
$attachments = preg_split('/\R/', $row['attachments']);
Upvotes: 1