带有变量的php邮件

I am attempting to setup a mail script which will first run a simple select from mysql, and use these array variables in the message. However all the variables are not output to the message body, only one row of variables. Here is my script:

    $sql1 = "SELECT * FROM videos WHERE checked_out = '1'"; 
    $result1 = $dbLink->query($sql1); 
              while($row1 = $result1->fetch_assoc()) {
    $name = $row1['name'];
    $tape_no = $row1['tape_no'];
    $member_name = $row1['member_name'];
    $date_out = date("F j, Y", strtotime($row1['date_out']));
              }

//email function to administrator
$to = "nouser@mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please@mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

appreciate any insight anyone cares to share on this. Thanks, --Matt

thats because you're while keeps overwriting the variables, so it'l get only the last one. You might want to just build the $message variable in the while loop by doing. that will send 1 email with everything

 $sql1 = "SELECT * FROM videos WHERE checked_out = '1'"; 
    $result1 = $dbLink->query($sql1); 
              while($row1 = $result1->fetch_assoc()) {
    $name = $row1['name'];
    $tape_no = $row1['tape_no'];
    $member_name = $row1['member_name'];
    $date_out = date("F j, Y", strtotime($row1['date_out']));
    $message .= "$name $tape_no $date_out $member_name"
              }

//email function to administrator
$to = "nouser@mail.com";
$subject = "Daily Video Rental Summary";
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please@mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

You are overwriting your variables in the loop so $name, etc. will contain the values found in the last row after the loop.

What you could do is construct your message in the loop:

$message = '';
while($row1 = $result1->fetch_assoc()) {
    $message .= $row1[...];    // whatever you need
}

Try this instead:

 $sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
 $result1 = $dbLink - > query($sql1);
 while ($row1 = $result1 - > fetch_assoc()) {
     $name = $row1['name'];
     $tape_no = $row1['tape_no'];
     $member_name = $row1['member_name'];
     $date_out = date("F j, Y", strtotime($row1['date_out']));
     //email function to administrator
     $to = "nouser@mail.com";
     $subject = "Daily Video Rental Summary";
     $message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
     $from = "no_replies_please@mail.com";
     $headers = "From:".$from;
     mail($to, $subject, $message, $headers);
 }

This will do a mail per row.

That's because you're assigning the variables in the loop, but using them in the $message variable outside the loop. So your $message contains only the items from the last row/record. Try moving and appending values in the $message variable inside the while loop.

So it could be

while($row1 = $result1->fetch_assoc()) {
    //assign vars here
    $message .= "$name $tape_no $date_out $member_name
";
}
$message = "$message
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";

//assign other variables
//mail()

Hope that helps.

Try this:

 $message = NULL; 
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'"; 
$result1 = $dbLink->query($sql1); 
while($row1 = $result1->fetch_assoc()) { 
$name = $row1['name']; 
$tape_no = $row1['tape_no']; 
$member_name = $row1['member_name']; 
$date_out = date("F j, Y", strtotime($row1['date_out']));
 $message .= "$name $tape_no $date_out $member_name" } 

And remove the other $message variable under the loop. Notice the . before the = in the $message. This tells php to continually add on to the $message