dongwaner1367
dongwaner1367
2019-01-28 22:46

有多个数据库交互和电子邮件更新cron作业时遇到问题

已采纳

New server has latest php and MariaDB, existing outdated code on cron job with more than one call to and from database with email notification no longer returns expected results.

Moved to new server, website built years ago by volunteers, I have tried changing how php interacts with database using mysqli changes I have read about, worked on all other basic pages but multiple calls using sql1, sql2 etc not functioning.

<?php
$headers = "MIME-Version: 1.0
" ;
$headers .= "From: member@trbncom.com
";
$headers .= "Content-Type: text/html; charset=ISO-8859-1
";
$dbh=MySQLi_connect ("localhost", "trbncom_main", "password!") or die ('I cannot connect to the database because: ' . MySQLi_error());
MySQLi_select_db ("trbncom_main");
$sql="select * from members where active='on' and mmonth='$renewalmonth' and mdate='$renewaldate'";
$result=MySQLi_query($sql); 
while($myrow=MySQLi_fetch_array($result))
{
$chapter=$myrow["chapter"];
$mdate=$myrow["mdate"];
$mmonth=$myrow["mmonth"];
$myear=$myrow["myear"];
$lname=$myrow["lname"];
$fname=$myrow["fname"];
$firstnotice=$myrow["firstnotice"];
$sent1st=$myrow["sent1st"];
$sent2nd=$myrow["sent2nd"];
$sentoverdue=$myrow["sentoverdue"];

echo("<br>$lname, $fname<br>Joined on $mmonth, $mdate, $myear<br>$chapter, CID= $cid");

    $sql2="select lname, fname, email from members where cid='$cid' and active='on' and admin='pres'";
    $result2=MySQLi_query($sql2); 
    while($myrow2=MySQLi_fetch_array($result2))
    {
    $fname2=$myrow2["fname"];
    $lname2=$myrow2["lname"];
    $email2=$myrow2["email"];

echo("<br>$lname2, $fname2<br>$email2");

mail("member@trbncom.com","$fname $lname Membership is up for Renewal","<span style='color: #000080; font-size:12px; font-family: tahoma,arial,helvetica,sans-serif;'>Hello $fname<br><br>

Another year has come and gone and it's time to renew your membership
Sincerely<br>
Membership Committee<br>
</span>","$headers");

$sent=date("d M Y");
$sql3="update members set sent1st= '$sent', firstnotice='yes' where mid=$mid"; 
$result3=MySQLi_query($sql3);
}}
?>

The file does discover members with due dates on this day, it does email the list of members to me, it does not retrieve the sql2 query or update the table in the sql3 update. I dumped all my previous mysqli update attempts and reverted to the original code that worked before update, and have displayed it.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxuanma4357 douxuanma4357 2年前

    When using MySQLi_query you need to pass also the database handle $dbh, or link, that you established with the call to MySQLi_connect.

    So, for example:

    $dbh = MySQLi_connect ("localhost", "trbncom_main", "password!") or die ('I cannot connect to the database because: ' . MySQLi_error());
    
    MySQLi_select_db ($dbh, "trbncom_main");
    
    $sql="select * from members where active='on' and mmonth='$renewalmonth' and mdate='$renewaldate'";
    
    $result=MySQLi_query($dbh, $sql); 
    
    ...
    

    Check documentation carefully: this is easy to miss. Hope this helps.

    点赞 评论 复制链接分享