如果表的记录为空,则不应发送电子邮件

时间:2018-06-26 05:49:23

标签: php oracle

我正在处理一个PHP项目,仅当表中有记录时,才需要将表的数据发送给某些收件人,否则不应该发送电子邮件。

请纠正我的“ if”条件,因为我是php新手。

 // if record is null then email should not be sent.
     $sql="SELECT * FROM tmp_Roshan_line T ";
     $result = odbc_exec($connect_cc, $sql) or die("Couldn't execute query! ".odbc_errormsg());

      if ($result!=NULL){

//Sending Email
$htmlbody ="
<head>
<style>
table {
    border-collapse: collapse;
    border: 0px;
    width: 20%;
}

th, td {
    text-align: left;
    padding: 3px;
    font-size:12px;
    border: 0px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #68a936;
    color: Black;
    border: 0px;
}
</style>
</head>";
$htmlbody .= "<p style='font-size:14;'>Dear Roshan Team,<br><br>
Following lines are dedected as 100% simbox lines by our detection tool, please block them and confirm with us. <b>"."</b></p><br><br>";



$htmlbody .= "

3 个答案:

答案 0 :(得分:1)

检索记录 odbc_fetch_row()函数用于从结果集中返回记录。如果能够返回行,则此函数返回true,否则返回false。

$sql="SELECT * FROM tmp_Roshan_line T";
$rs=odbc_exec($connect_cc,$sql);
if (!$rs)
  {
  exit("Error in SQL");
  }


while (odbc_fetch_row($rs))
{

 //Sending Email
$htmlbody ="
<head>
<style>
table {
    border-collapse: collapse;
    border: 0px;
    width: 20%;
}

th, td {
    text-align: left;
    padding: 3px;
    font-size:12px;
    border: 0px;
}

tr:nth-child(even){background-color: #f2f2f2}

th {
    background-color: #68a936;
    color: Black;
    border: 0px;
}
</style>
</head>";
$htmlbody .= "<p style='font-size:14;'>Dear Roshan Team,<br><br>
Following lines are dedected as 100% simbox lines by our detection tool, please block them and confirm with us. <b>"."</b></p><br><br>";


// send email code 
}
odbc_close($conn);

答案 1 :(得分:0)

您可以简单地检查条件,而无需将其与NULL比较。

if ($result){
   # condition goes here
 }

或者如果您真的想将其与空值进行比较,则可以这样做 通过使用php is_null函数

if (!is_null($result)) {
   # condition goes here
}

答案 2 :(得分:0)

使用

if (odbc_num_rows($result)>0){
 //send mail
  }
else{
      //dont send mail
    }

orbc_exec()仅在运行查询时出错时,此处将返回false。即使查询执行没有结果,它也应返回true。最好的方法是计算返回的行数。为此,您需要使用[odbc_num_rows()][1]