2011-08-15 7 views
0

私は単純なmailto関数を使用して、dbリストのすべての名前に対して適用/実行したいと考えています。それは働いていないので、私はそれからのすべてのメールのものを取り出して、whileループはデシベルで働いていたことを確認するためにテストするために、このWhileLoop from mysql dbリスト

<?php 


$connect2db = mysqli_connect('127.0.0.1','root','pass','dbnamehere'); 
if(!$connect2db){ 
    die("Sorry but theres a connection to database error" . mysqli_error); 
} 

$sn_query = "SELECT * FROM email_list"; 

$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error); 
$sn_rowSelect = mysqli_fetch_array($sn_queryResult); 
$to = $sn_rowSelect; 

?> 

<br/><br/> 
////lower part on page //////<br/><br/> 


<?php 
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult)) { 
echo "hello there" . " " . $sn_rowSelect['firstname'] . " <br/>"; 
} 
?> 

をした今、この作品、それが出てすべての私のデシベルと版画を通じてgoessデータベースリストの私の最初の名前。私のnoob脳では、私はエコーラインを削除し、適切なmailto情報を入力すると、前と同じようにループすると思うが、各名前にメールを送る。

<?php 
$sn_query = "SELECT email FROM email_list"; 

$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error); 

$sn_rowSelect = mysqli_fetch_array($sn_queryResult); 

$to = implode(",",$sn_rowSelect); 
$from = $_POST['sender']; 
$subject = $_POST['subj']; 
$mssg = $_POST['message']; 
$headers = "MIME-Version: 1.0rn"; 
$headers .= "From: $from\r\n"; 
$mailstatus = mail($to, $subject, $mssg, $headers); 

?> 

<br/><br/> 
//////////<br/><br/> 


<?php 
while($sn_rowSelect = mysqli_fetch_array($sn_queryResult)) { 
    $mailstatus; 

    if($mailstatus) { 
     echo "Success"; 
    }else{ 
     echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n"; 
    } 
} 



?> 

今、この電子メール私のリスト上の最初の名前ではなく、残り:ので、私はこれをしませんでした。 私は何のエラーも発生していないので、何が問題なのか分かりません。私はnum_rowsとifステートメントを試すつもりだったが、StackOverflowのどこか他の誰かが、whileループがそれ自身でそれを世話したので助けにならなかったと言った。 (私はそれをどちらかの方法で試して、それはまだ最初の名前だけをメールしました)私はここにしようとしているが、役に立たない。

答えて

3

ループ内でmail()関数を呼び出していません。あなたはそれを一度外に呼びます。代わりに次のようなものを試してみてください。

、行セットから引き出し、そしてmail()でそれを使用する(あなたがテストでファーストネームで行ったように)あなたは、データベースクエリから$toアドレスを取得したと仮定:また

while($sn_rowSelect = mysqli_fetch_array($sn_queryResult)) { 

    // Get the $to address: 
    $to = $sn_rowSelect['email']; 

    // Call mail() inside the loop. 
    $mailstatus = mail($to, $subject, $mssg, $headers); 

    if($mailstatus) { 
     echo "Success"; 
    }else{ 
     echo "There was a problem sending the mail. Check your code and make sure that the e-mail address $to is valid\n"; 
    } 
} 

。なお、以降のスクリプトの先頭にmysql_fetch_array()と呼ぶと、whileループは2番目の行から始まります。ループの前に発生するmysql_fetch_array()への最初の呼び出しを削除する必要があります。

$sn_queryResult = mysqli_query($connect2db, $sn_query) or die("Sorry but theres a connection to database error" . mysqli_error); 

// Don't want this... 
//$sn_rowSelect = mysqli_fetch_array($sn_queryResult); 
+0

私は理由を知っていませんが、それはうまく動いています。ありがとう。この行までは、 "whileループは2番目の行から開始します。ループの前に発生するmysql_fetch_array()への最初の呼び出しを削除する必要があります。"私はまだ、なぜか、私はまだnoob、悪い数字をそれlaterlol、再び、ありがとう – somdow