duanqing3026 2015-10-08 14:26
浏览 27
已采纳

MySQL回声只有一个元素

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 ?>
  • 写回答

1条回答 默认 最新

  • doudianhuo1129 2015-10-08 14:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?