I’m trying to fetch all subscriber email addresses from my database table and combine them into a single PHP string variable. This string will be used later for sending emails to multiple recipients.
The issue I’m facing is that my variable only contains one email address instead of all the records from the database. My table has around 20 email entries but only the first one gets stored.
Here’s my current code:
$query = "SELECT * FROM subscribers";
$result = mysqli_query($connection, $query);
if($result) {
if(mysqli_num_rows($result) == 1) {
while($data = mysqli_fetch_assoc($result)) {
$email_list = $email_list . $data["email_address"] . ", ";
}
}
}
I also attempted using arrays but couldn’t figure out how to add commas between the email addresses. What’s the correct approach to concatenate all email addresses from the database into a comma-separated string?
Hit this same issue building a newsletter system last year. Your problem’s that conditional check - you’re looking for exactly one row when you’ve got twenty records. Beyond fixing the row count, try mysqli_fetch_column() if you’re just grabbing emails. Way more direct than fetch_assoc() for single columns. Also, validate those emails before concatenating them, especially with user input. Learned this the hard way when bad emails killed my entire mailing function. The implode() approach others mentioned is cleaner for maintenance, but your string concatenation works fine once you ditch that broken condition.
youre checking for exactly 1 row with mysqli_num_rows($result) == 1, but you mentioned there are 20 emails in the table. thats why only the first one shows up. remove that condition or change it to > 0. also, initialize $email_list = "" before ur loop or you’ll get undefined variable warnings.
Quick fix - your mysqli_num_rows($result) == 1 is the problem. You’re telling it “only run if there’s exactly 1 row” but you’ve got 20 rows. Just use if($result) instead and ditch the row count check. The while loop handles everything automatically.
The issue arises from the condition mysqli_num_rows($result) == 1, which checks for exactly one row. Since you have 20 records, this check prevents the loop from executing. To fix this, change it to mysqli_num_rows($result) > 0 or remove the condition altogether since you’re already verifying the result’s existence. As for the array method, using implode() is a cleaner approach. Populate an array with email addresses and then join them:
$emails = array();
while($data = mysqli_fetch_assoc($result)) {
$emails[] = $data["email_address"];
}
$email_list = implode(", ", $emails);
This way, comma separation is handled automatically without worrying about trailing commas.
Had the same problem when I started with database queries. Yeah, that condition’s checking for exactly 1 row - that’s your main issue. But you’ve also got another problem: $email_list isn’t initialized, which throws PHP notices. I always trim the final result too since you’ll get a trailing comma and space. Here’s what works for me:
$email_list = “”;
$query = “SELECT email_address FROM subscribers”;
$result = mysqli_query($connection, $query);
if($result && mysqli_num_rows($result) > 0) {
while($data = mysqli_fetch_assoc($result)) {
$email_list .= $data[“email_address”] . ", ";
}
$email_list = rtrim($email_list, ", ");
}
Also changed the SELECT to grab just email_address instead of using asterisk - way more efficient when you only need specific data.
This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.