如何使用php从mysql数据库中获取数据

时间:2013-11-29 14:16:33

标签: php mysql

如何使用php从mysql数据库中获取数据并触发邮件到我从mysql数据库中获取的那些电子邮件ID,并且还希望在邮件主题中包含另外两个数据。无法添加code.can任何一个帮我如何添加代码?

2 个答案:

答案 0 :(得分:1)

尝试使用如下的select语句:

<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM Persons");

while($row = mysqli_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br>";
  }

mysqli_close($con);
?>

然后使用邮件系统,例如:

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

参考文献: http://php.net/manual/en/function.mail.php http://www.w3schools.com/php/php_mysql_select.asp

这将允许您从数据库和电子邮件信息中选择数据。

答案 1 :(得分:1)

这是一个示例代码:

$headers = 'From: youremailgoes@here.com' . "\r\n";
$mysqli = new mysqli("localhost", "user", "password", "database");
$output = $mysqli->prepare("SELECT id FROM table WHERE x=?");
$output->bind_param("s", $parameter);
$output->execute();
$output->store_result();
$output->bind_result($id);
while ($output->fetch()) 
{
    mail($to, $subject, $emailcontent, $additionalheader);
}
$output->close();

看看这些链接:
1. http://php.net/manual/de/mysqli.quickstart.prepared-statements.php
2. http://us3.php.net/manual/de/book.mail.php