Php database

bunglebrown

New Member
Messages
157
Reaction score
0
Points
0
Below is a script I'm using to retrieve a record from a mySQL database and then use it to send a message to.

I'm having some trouble however with putting the retrieved table entry into email part of the code.

PHP:
<?php 
//Change these
$db = 'my_database';
$user = 'my_user';
$password = 'my_password';

if(!mysql_connect('localhost', $user, $password)) {
 exit(mysql_error());
}
if(!mysql_select_db($db)) exit(mysql_error());

// Get a specific result from the "example" table
$result = mysql_query("SELECT * FROM `my_table`") or die(mysql_error());  

// Loop to get multiple results
while($row = mysql_fetch_array($result)){
echo "Name: ".$row['name'];
echo "Email: ".$row['email'];
}

?>

<?php
$from = "my_email";
$subject = "my_subject";
//Begin HTML Email Message
$message = "my_message";
 
echo '  ';
//end of message
$headers  = "From: $from\r\n";
$headers = "Content-type: text/html\r\n";
$to = "$row['email']";
 
mail($to, $subject, $message, $headers); 
 
exit();
?>

Does anyone have any knowledge on what I'm doing wrong it would be great to hear from you } thanks
 

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
I think you wanted to send the database data to all the emails reterived. here is what i modified.
PHP:
<?php  
 //Change these 
 $db = 'my_database'; 
 $user = 'my_user'; 
 $password = 'my_password'; 
 if(!mysql_connect('localhost', $user, $password)) { 
   exit(mysql_error()); 
 } 
 
 if(!mysql_select_db($db)) exit(mysql_error()); 
 // Get a specific result from the "example" table 
 
 $result = mysql_query("SELECT * FROM `my_table`") or die(mysql_error());   
 // Loop to get multiple results 
 
 $from = "my_email"; 
 $subject = "my_subject"; 
 while($row = mysql_fetch_array($result)){ 
  $message = $row['name']; 
  $headers = "MIME-Version: 1.0\r\n";
  $headers.= "Content-Type: text/html;";
  $headers.= "Charset=iso-8859-1\r\n";
  $headers.= "From: ".$from; 
  $headers = "Content-type: text/html\r\n"; 
  $to = $row['email']; 
  mail($to, $subject, $message, $headers);  
 } 
 exit(); 
?>

this code will send the name reterived from the database to the email address reterived from the database.

if this is not similar to your requirement. Post your exact requirement like what data from database you need to send and to which email id. Whether to a single email id or to email ids that will be reterived from the database.
 
Last edited:

balaji2u

New Member
Messages
410
Reaction score
2
Points
0
Sunil is correct .but the headers variables are with out the . {DOT} which will cause it to not append the previous stored strings?
or is it will not create problems .i think sunil leave this because may be php automatically appends the string if im right!!!
 

sunils

New Member
Messages
2,266
Reaction score
0
Points
0
Thanks balaji for bringing to the my notice... you should concatenate it otherwise the previous values will be over written
 
Top