I have a email script that sends out information from the mysql database with the following code, but it only sends out one name and ID, but in the email it should send all 20 names and IDs. My question how can I get this fix and working right. Thanks for any help.

//retreive questions from database and put into question box
$query2 = "SELECT `studentID`,`studentName` FROM `quiz1question`";

$question2 = mysql_query($query2);
 while($row = mysql_fetch_array($question2)){

$studentName = $row['studentName'];
$studentID = $row['studentID'];

$msg .= '<p>Student Name: '.$studentName.' or Student ID: '.$studentID.'</p>';


// ------------------------------------------------------------

$to = "cc-wp@hotmail.com";

$subject = "CT-80 Quiz/Test Results";

$from = "akamerc@hotmail.com";

$headers ="From: $from\n";

$headers .= "MIME-Version: 1.0\n";

$headers .= "Content-type: text/html; charset=iso-8859-1 \n";

if(mail($to,$subject,$msg,$headers)) {

    echo "success";

    exit();

} else {       

    echo "failed";

    exit();

}
?>

<?php } ?>

exit terminates the script, so the while loop just stops after the first mail.

Thanks for the reply what I am trying to do is this in the email. Database has 20 name or whatever. In the email should look like this with the following info:

  1. **
  2. **
  3. **

and so on. It just shows 1.** and thats it. when I take out exit(); it sends me mass emails.

Then mail should be outside of the loop, and you build the body text inside the loop.

Thanks for the help again and also it works. Here's the result.

//retreive questions from database and put into question box
$query2 = "SELECT * FROM `quiz1question` `student`";

$question2 = mysql_query($query2);
 while($row = mysql_fetch_array($question2)){

$id = $row['id'];
$question = $row['question'];
$quizAnswer = $row['quizAnswer'];
$aee = $row['aee'];
$bee = $row['bee'];
$cee = $row['cee'];
$dee = $row['dee'];
$studentName = $row['studentName'];
$studentID = $row['studentID'];

$msg .= 'Student Name: '.$studentName.' <br />Student ID: '.$studentID.' <br />'.$id.' '.$question.' <br />A. '.$aee.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />B. '.$bee.'&nbsp;&nbsp;&nbsp;&nbsp;<br />C. '.$cee.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />D. '.$dee.'<br />Your Answer:<br />
Correct Answer: '.$quizAnswer.'<br /><br />';

?>
<?php } ?>
<?PHP
// ------------------------------------------------------------

$to = "cc-wp@hotmail.com";

$subject = "CT-80 Quiz/Test Results";

$from = "akamerc@hotmail.com";

$headers ="From: $from\n";

$headers .= "MIME-Version: 1.0\n";

$headers .= "Content-type: text/html; charset=iso-8859-1 \n";

if(mail($to,$subject,$msg,$headers)) {

    echo "success";

    exit();

} else {       

    echo "failed";

    exit();

}
?>

In my email it looks like this, but how can I get it to just show the Student Name and Student Id only on the top and not on all of it like its doing on nubmer 2.

Student Name: 
Student ID: 
1 Whats black and white? 
A. AAA        
B. dd    
C. dsds            
D. cop car
Your Answer:
Correct Answer: cop car

Student Name: 
Student ID: 
2 What process determines the identity of a user? 
A. AAA        
B. dsdsd    
C. DDD            
D. cccc
Your Answer:
Correct Answer: cccc

Only output it the first in the loop. Use a counter and an if statement.

Sorry but I am lost now. How would I write a if statement and where would I put it.

The if statement would be inside the loop, around the output of the student information.

I think this looks wrong.

if ( $studentName == "studentName" )
   {
echo "Student Name: '$studentName'";
$msg .= ''.$id.'.  '.$question.' <br />A. '.$aee.'        <br />B. '.$bee.'    <br />C. '.$cee.'            <br />D. '.$dee.'<br />Your Answer:<br />
Correct Answer: '.$quizAnswer.'<br /><br />';
 }

Trying to get it like this

 Student Name: 

    1. Whats black and white? 
    A. AAA        
    B. dd    
    C. dsds            
    D. cop car
    Your Answer:
    Correct Answer: cop car

    2. What process determines the identity of a user? 
    A. AAA        
    B. dsdsd    
    C. DDD            
    D. cccc
    Your Answer:
    Correct Answer: cccc

and not like this

Student Name: 

1. Whats black and white? 
A. AAA        
B. dd    
C. dsds            
D. cop car
Your Answer:
Correct Answer: cop car

Student Name: 

2. What process determines the identity of a user? 
A. AAA        
B. dsdsd    
C. DDD            
D. cccc
Your Answer:
Correct Answer: cccc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.