简体   繁体   中英

MySQL echos only one elemen

I've a problem with a MySQL script. The script should echo every element out of the table where "gruppe" is $gruppe. But I am only getting one single output.

<?php

if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {

    $gruppe = $_POST["gruppe"];
    $mail = $_POST["mail"];
    $betreff = $_POST["betreff"];
    $nachricht = $_POST["nachricht"];

    if (!empty($nachricht)) {

$sql = new rex_sql;

$sql->debugsql = 0; //Ausgabe Query

$sql->setQuery("SELECT * FROM $db_users WHERE gruppe = '$gruppe'");

for($i=0;$i<$sql->getRows();$i++)
{

$id_mail = $sql->getValue("id");
$mail_mail = $sql->getValue("email");

$ausgabe_mail = $mail_mail;

$sql->next();
}

}

}

?>

<?php echo $ausgabe_mail ?>

Your echo-statement is outside the loop. The loop fetches all email addresses, one by one, and stores them in $mail_mail and $ausgabe_mail . Each iteration of the loop overwrites the previous contents of both variables.

After the loop ends, you echo the last value of $ausgabe_email .

Retry with the echo inside the loop:

<?php
if ( $_SERVER["REQUEST_METHOD"] == 'POST' ) {
    $gruppe = $_POST["gruppe"];
    $mail = $_POST["mail"];
    $betreff = $_POST["betreff"];
    $nachricht = $_POST["nachricht"];

    if (!empty($nachricht)) {
      $sql = new rex_sql;
      $sql->debugsql = 0; //Ausgabe Query
      $sql->setQuery("SELECT * FROM $db_users WHERE gruppe = '$gruppe'");

      for($i=0;$i<$sql->getRows();$i++) {
        $id_mail = $sql->getValue("id");
        $mail_mail = $sql->getValue("email");

        $ausgabe_mail .= ',' . $mail_mail;
        $sql->next();
      }
      echo $ausgabe_mail;
    }
}
?>

EDIT: as clarified, expanded the example with string concatenation and moved the echo outside the loop again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM