基于SQL查询结果无法正常工作的电子邮件通知

时间:2012-06-30 15:42:30

标签: php sql email

这是我的PHP:

#!/cron.php
<?php

$dbconnect = new mysqli('localhost','root','root','serrano');
$query = "SELECT `app`,`email`,`accept` FROM `requests` WHERE `accept`=1";

$result = mysql_query($query);

if($result && $result->num_rows>=1) {
    $subject = "Your application has been accepted for review.";
    $message='Congratulations. Your application "';

    while($row=$result->fetch_assoc()) {
        $message.="{$row['name']}\"";
        $email="{$row['email']}";
    }

    $message.="has been accepted for review at redacted.";
    if(mail($email, $subject, $message)) {
      //mail successfully sent
    } else {
        echo "fail";
    }
  }
?>

据我所知,查询工作正常。它返回数据库中的相关行。我的PHP有什么用?

代码更新1

这是更新的PHP,更正了mysqli错误。然而它仍然会返回一个错误:

PHP致命错误:在第5行的[...]中的非对象上调用成员函数query()

#!/cron.php
<?php

$dbconnect = new mysqli('localhost','root','root','serrano');
$result->query('SELECT `app`,`email`,`accept` FROM `requests` WHERE `accept`=1');

if($result && $result->num_rows>=1) {
$subject = "Your application has been accepted for review.";
$message='Congratulations. Your application "';

while($row=$result->fetch_assoc()) {
    $message.="{$row['name']}\"";
    $email="{$row['email']}";
}

$message.="has been accepted for review at redacted.";
if(mail($email, $subject, $message)) {
  //mail successfully sent
} else {
    echo "fail";
}
  }
?>

工作代码

好的,这是工作代码。只需对连接和运行查询的方式进行一些调整:

#!/cron.php
<?php


$link = mysqli_connect('localhost','root','root','serrano');

$result = mysqli_query($link, 'SELECT `app`,`email`,`accept` FROM `requests` WHERE  `accept`=1');

if($result->num_rows >= 1){
$subject = "Your application has been accepted for review.";
$message='Congratulations. Your application "';

while($row=$result->fetch_assoc()) {
    $message.="{$row['app']}\"";
    $email="{$row['email']}";
}

$message.="has been accepted for review at redacted.";
if(mail($email, $subject, $message)) {
  //mail successfully sent
} else {
    echo "fail";
}
 }
?>

1 个答案:

答案 0 :(得分:2)

您正在使用mysqli,但同时使用mysql_query

应该是$dbconnect->query()而不是mysql_query