0

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 = "[email protected]";
$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 = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

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

0

5 Answers 5

2

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 = "[email protected]";
     $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 = "[email protected]";
     $headers = "From:".$from;
     mail($to, $subject, $message, $headers);
 }

This will do a mail per row.

Sign up to request clarification or add additional context in comments.

2 Comments

That´s going to be one unhappy administrator :-)
@jeroen -- thats not my problem ^_^ (or is it...?)
1

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 = "[email protected]";
$subject = "Daily Video Rental Summary";
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);

1 Comment

you have some quotation mark error somewhere, and the message would keep getting rewritten - so this is also wrong...
0

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
}

Comments

0

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

Comments

0

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\n";
}
$message = "$message
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";

//assign other variables
//mail()

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.