Hello world,

I have a DB in mysql, and want to create a code that will select all this DB data ( there are 10 records) and send it via email (created send email in php).

Anyone have a such example please ?Could you help me =?

Thanks in advance.

Member Avatar for Rkeast

After connecting to your database all you'd need to do is do a SELECT and do a while loop to add each entries data to an email message.

Here is an example, with syntax for MySQL:

$to      = 'youremail@example.com';
$subject = 'Your Subject';
		
$message = <<<EOT
<html>
<head>
  <title>Your Subject</title>
</head>
<body>
<p>Here is the data from the Example table:</p>
<table>
<tbody>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
</tr>
EOT;

$sql = "SELECT * FROM `your_table`";
$result = mysql_query($sql);
while($data = mysql_fetch_array($result))
{
$message .= '
<tr>
<td>.'$data['field_1'].'</td>
<td>.'$data['field_2'].'</td>
<td>.'$data['field_3'].'</td>
</tr>
';
}

$message .= <<<EOT
</tbody>
</table>
</body>
</html>
EOT;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Example <youremail@example.com>' . "\r\n";

mail($to, $subject, $message, $headers);

After connecting to your database all you'd need to do is do a SELECT and do a while loop to add each entries data to an email message.

Here is an example, with syntax for MySQL:

$to      = 'youremail@example.com';
$subject = 'Your Subject';
		
$message = <<<EOT
<html>
<head>
  <title>Your Subject</title>
</head>
<body>
<p>Here is the data from the Example table:</p>
<table>
<tbody>
<tr>
<th>Field 1</th>
<th>Field 2</th>
<th>Field 3</th>
</tr>
EOT;

$sql = "SELECT * FROM `your_table`";
$result = mysql_query($sql);
while($data = mysql_fetch_array($result))
{
$message .= '
<tr>
<td>.'$data['field_1'].'</td>
<td>.'$data['field_2'].'</td>
<td>.'$data['field_3'].'</td>
</tr>
';
}

$message .= <<<EOT
</tbody>
</table>
</body>
</html>
EOT;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Example <youremail@example.com>' . "\r\n";

mail($to, $subject, $message, $headers);

Greate, God bless you, its work very good. Thank you very much

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.