0

I want to get all email addresses from MySQL database and fill that into a PHP variable. PHP variable should be used to send mail to all addresses.

The problem is that only one email is written in the variable...There should be 20 of them...

$q = "SELECT * FROM email_subscribe";
        $r = mysqli_query($dbc, $q);
        if($r) {
            if(mysqli_num_rows($r) == 1) {
                while($row = mysqli_fetch_assoc($r)) {
                    $recivers = $recivers . $row["email"] . ", ";
                }
            }
        }

I tried to use the array but then I can't put a comma ( , ).

Nina
  • 57
  • 8
  • First define `$recivers =[]`. then just fetch all the emails in array like `$recivers[] = $row["email"]` and then `$all_email = implode(", ",$recivers)` now you have all the email address in `$all_email ` – Dhaval Purohit Aug 31 '19 at 09:37
  • **Warning**: but if you put all these emails in `to` of `mail` all subscribers have all other `emails` – Dhaval Purohit Aug 31 '19 at 09:39
  • I still only get one email in ```echo $all_email``` ...which is then an option to email each one individually with for?? – Nina Aug 31 '19 at 09:45
  • it is because `if(mysqli_num_rows($r) == 1) {` what is it for? – Dhaval Purohit Aug 31 '19 at 09:46
  • ```if(mysqli_num_rows($r) > 0)``` the same is happening..I use this to check if there are emails in the database – Nina Aug 31 '19 at 09:49

1 Answers1

0

use concat otherwise you'll get only last.

    $recivers = '';
    if($r) {
                if(mysqli_num_rows($r) == 1) {
                    while($row = mysqli_fetch_assoc($r)) {
                        $recivers .= $row["email"]. ", ";
                    }
                }
     }
echo $recivers;
echo rtrim($recivers, ','); //remove last comma.

If you want it in array then.

$recivers  = array();   
if($r) {
        if(mysqli_num_rows($r) == 1) {
            while($row = mysqli_fetch_assoc($r)) {
                $recivers[] = $row["email"];
            }
        }
}
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49