使用multi_query在数组中运行多个查询

时间:2018-01-27 12:40:15

标签: php mysql json

我有一个文本文件,我打开它并将其转换为对象,我想当条件为真时将查询语句放在数组名称autocharges然后foreach in array并运行multi_query查询。我的问题是它没有在表中插入两行,它只插入一行并运行与我的文本文件的第一行相关的查询

$file = fopen(__DIR__.'/acf.txt', "r" );
while ( !feof( $file ) ) {
$content = json_decode(fgets( $file ));
        $convertedObjToArr = get_object_vars($content);
        $event_type = $convertedObjToArr['event-type'];
        $status = $convertedObjToArr['status'];
        $date = $convertedObjToArr['datetime'];
        if($status == '0' AND $event_type == '1.5'){
            $sql = " INSERT INTO auto_charge(phone,charged_at,created_at,updated_at) VALUES ('$content->msisdn','$date','$now','$now');";
            $autoCharges[ $content->sid ] .= $sql;
            $c++;
        }
}
foreach ( $autoCharges as $sid => $query ) {
    $result = $conn->multi_query( $query );
    $conn->close();
}

这是我的文字:

{"sid":7790,"trans-id":"PROV_151678481611490383229","status":"0","base-price-point":"3000","msisdn":"989120572595","keyword":"","validity":1,"next_renewal_date":"2018-01-25 12:36:56.634","shortcode":"","billed-price-point":"3000","trans-status":0,"chargeCode":"MEHRENCMEHCLAS3000","datetime":"2018-01-24 12:37:07","event-type":"1.5","channel":"system"}
{"sid":4567,"trans-id":"PROV_151678481611490383229","status":"0","base-price-point":"3000","msisdn":"989107786574","keyword":"","validity":1,"next_renewal_date":"2018-01-25 12:36:56.634","shortcode":"","billed-price-point":"3000","trans-status":0,"chargeCode":"MEHRENCMEHCLAS3000","datetime":"2018-01-24 12:37:07","event-type":"1.5","channel":"system"}

0 个答案:

没有答案