php - 选择条件 SQL 语句

标签 php mysql json

我正在将数据发布到 URL 以发送感谢消息。我选择的详细信息来自一个名为 readtextfilejson 的表。我从那里向用户发送感谢消息。

但是,我无法选择尚未收到短信的唯一用户的详细信息。我的代码是选择所有数据并一次又一次地循环发布给所有用户。从而向用户发送多条ThankYouMessages。

我添加了一个名为ThankyouMessage 的新列,默认情况下=“未发送”。 这样,当我的脚本运行时,它会将ThankYouMessage 列更新为 = 'SENT',以便我的脚本只能选择尚未收到感谢消息的用户的详细信息。

因此我不会一次又一次地重新发送相同的消息。请查看下面的脚本并帮助我解决此问题。

我的表结构:

enter image description here

 <?php

        $data = (string) file_get_contents($file);

        //echo $data;

        $data = str_replace('//Confirmation Respose', '', $data);
        $data = str_replace('// Validation Response', '', $data);
        $data = str_replace(' ', '', $data);
        $data = preg_replace('/\s+/S', " ", $data);
        $data = trim($data);
        $pattern = '/\s*/m';
        $replace = '';

        $testString = $data;

        $removedWhitespace = preg_replace( $pattern, $replace,$testString );
        $removedWhitespace2 = str_replace (' ', '', $testString);

        $getAllData = explode('}{', $removedWhitespace2);
        foreach ($getAllData as $row) {
           $row = str_replace('{', '', $row);

           $rowData = explode(',"', $row);
           $rowData = explode(',"', $row);
           $columnValues = array();
           $chkTransId = '';



           foreach ($rowData as $value) {
              $newVal = explode(':', $value);
              $key = str_replace('"', '', $newVal[0]);
              $val = str_replace('"', '', $newVal[1]);
              $val = trim($val);

              $columnValues[] = ($val) ? "'$val'": "''";
              if($key == 'TransID'){
                 $chkTransId = $val;
              }
           }
           if($chkTransId == ''){
              continue;
           }
           ////THIS IS THE SECTION AM HAVING PROBLEMS WITH - I WANT TO
           ////SELECT ONLY THE DATA WHERE THE COLUMN  WHERE thankyoumessage =            
            ///// 'NOT SENT'
           $chkSql = "select * from `readtextfilejson`where TransID='$chkTransId'";

           $getResult = mysqli_query($con, $chkSql); 
           $getCount = mysqli_num_rows($getResult);
           $row = mysqli_fetch_object($getResult);

$text = "Dear ". $row->FirstName ." Your Payment of ". $row->TransAmount ." to XXXXX was Received Succesfully. Confirmation Code: ". $row->TransID  ."";
   $destination = array("messageId"=>"$product_id","to"=>"$row->MSISDN");
   $product_id=uniqid();



 $notifyUrl = "URL";
    $notifyContentType = "application/json";
    $callbackData = 'eHostOnlineCodeCheck125690';
    $username = "USERNAME";
    $password = "PASSWORD";
    $postUrl = "POSTURL";

    $message = array("from" => "USERNAME",
            "destinations" => $destination,
            "text" => $text,
            "bulkId" => "",
        "notifyUrl" => $notifyUrl,
        "flash" => "false",
            "notifyContentType" => $notifyContentType,
        "callbackData" => $callbackData);       
    $postData = array("messages" => array($message));
    $postDataJson = json_encode($postData);
    //Submit all data to SMS server
    $ch = curl_init();
    $header = array("Content-Type:application/json", "Accept:application/json");
    curl_setopt($ch, CURLOPT_URL, $postUrl);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataJson);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // response of the POST request
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $responseBody = json_decode($response);

    curl_close($ch);

echo "<pre>";
print_r($row);
print_r($responseBody);
echo "</pre>";



 $Sql = "UPDATE readtextfilejson SET thankyoumessage = 'SENT' WHERE thankyoumessage = 'not sent'";
  mysqli_query($con, $Sql) or die(mysqli_error($con)); 




   if($getCount > 0){
      continue;
   }

   $columnValues = implode(',', $columnValues);
   $sql = "INSERT INTO `readtextfilejson`(`TransactionType`, `TransID`, `TransTime`, `TransAmount`, `BusinessShortCode`, `BillRefNumber`, `InvoiceNumber`, `OrgAccountBalance`, `ThirdPartyTransID`, `MSISDN`, `FirstName`, `MiddleName`, `LastName`) VALUES (".$columnValues.")";

   mysqli_query($con, $sql) or die(mysqli_error($con)); 
}
echo 'Data inserted successfully';
?>

最佳答案

2 个可能的问题。

1) 无论thankyoumessage 设置为何,您都会选择该交易。您可能需要向第一个 SQL 的 where 子句添加一个条件

SELECT * FROM `readtextfilejson`
WHERE TransID = '$chkTransId' AND thankyoumessage = 'not sent'

2) 当您将交易thankyoumessage 更新为“SENT”时,您正在设置所有 交易,因为您的更新语句缺少交易ID。您可能需要添加它。

UPDATE readtextfilejson 
SET thankyoumessage = 'SENT' 
WHERE thankyoumessage = 'not sent' AND TransID = '$chkTransId'

并且由于您希望将其设置为 SENT,无论它之前是什么,因此您可能也不需要thankyoumessage 检查。

UPDATE readtextfilejson 
SET thankyoumessage = 'SENT' 
WHERE TransID = '$chkTransId'

关于php - 选择条件 SQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52223529/

相关文章:

javascript - Angular 6 : Convert string to array of a custom object

php - 遍历数组并获取键和值

php - 将 URL ID 更改为简单文本

json.Marshal 返回奇怪的结果

mysql - 当结果== null时,Yii2从SQL命令获取列名

MySQL - 按数字或字母查询?

json - 我应该使用spray 还是play 来进行高度可扩展和高效的rest json 处理?

php - 在PHP中将整数转换为X个字符串

php - 请求 URI 太大

php - NSMutableURLRequest : setHTTPMethod 的解释