dongwaner1367 2019-01-28 22:46
浏览 41
已采纳

有多个数据库交互和电子邮件更新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 2019-01-28 23:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题