0

I am trying to create a list of emails separated by coma from a prepared statement.

                $prep_stmt = "SELECT m.email
                                FROM roster_par_membre rm
                                LEFT JOIN membre m 
                                        ON m.id_membre = rm.id_membre                           
                                WHERE id_roster = ? 
                                ";
                $stmt = $mysqli->prepare($prep_stmt);           
                if ($stmt) {
                    $stmt->bind_param('i', $roster_id);
                    $stmt->execute();
                    $stmt->store_result();
                    // get variables from result.
                    $stmt->bind_result($emails_roster);
                    $list = array(); (added)
                    while ($stmt->fetch()){
                        $list[] = $emails_roster;
                    }   
                    $list = implode(',', $list);

The output should be a list of emails without separations. How can I get a list with coma that I can directly use to send an email to this list? At the moment I get a fatal Error: Fatal error: [] operator not supported for strings (for the line inside the while!

7
  • 3
    I would just store the emails in an array and then implode(',',$emailArray) after the loop. Commented Jan 13, 2015 at 18:34
  • 1
    Use implode() Commented Jan 13, 2015 at 18:34
  • 1
    Instead of printing the value in the loop, add it into an array. Then implode() that array. Commented Jan 13, 2015 at 18:34
  • @RocketHazmat Thanks for the answer. It is what I am trying to do without success Commented Jan 13, 2015 at 18:49
  • 1
    Why not select group_concat(',',m.email)? Less bloating, all the code comes at once and does your requirement. Commented Jan 13, 2015 at 18:57

2 Answers 2

2

Try implode:

while ($stmt->fetch()){
   $emails[] = $emails_roster;
}
$emails = implode(',', $emails);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the answer. I should be doing something wrong but when I copy your code I get: Fatal error: [] operator not supported for strings
You've previously assigned a string value to $emails then. Try using $list or something else that you haven't already defined.
Dunno, you're doing something else wrong: 3v4l.org/H2Hdq You aren't doing echo $emails[]; are you?
Thanks a lot for helping out my exact code: $prep_stmt = "SELECT m.email FROM roster_par_membre rm LEFT JOIN membre m ON m.id_membre = rm.id_membre WHERE id_roster = ? "; $stmt = $mysqli->prepare($prep_stmt); if ($stmt) { $stmt->bind_param('i', $roster_id); $stmt->execute(); $stmt->store_result(); // get variables from result. $stmt->bind_result($emails_roster); while ($stmt->fetch()){ $list[] = $emails_roster; } $list = implode(',', $list);
OK found the solution; I needed to declare the Array before the while loop $list = array(); Thanks guys @AbraCadaver
1

Not sure if I understood it correctly, but if your $emails_roster contains an email then simply append to an array:

$emails = [];
while ($stmt->fetch()){
 $emails[] = $emails_roster;
}   

print_r($emails); 
echo implode(',', $emails);

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.