Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter一个查询多个语句_Php_Mysql_Codeigniter - Fatal编程技术网

Php CodeIgniter一个查询多个语句

Php CodeIgniter一个查询多个语句,php,mysql,codeigniter,Php,Mysql,Codeigniter,我使用CodeIgniter,当一个insert\u批处理不能完全工作时(插入的项目数与给定的项目数不同),我必须再次进行插入,使用insert ignore最大化通过过程的数量,而不会对现有项目产生错误。 当我使用这种方法时,我插入的数据类型不需要严格遵守给定的项数和放入数据库的项数。最大化是方法 a)尽可能多地使用insert_batchb)在失败时,使用变通方法,同时最大限度地减少不必要的请求,正确的方法是什么 感谢@q81提到的,您可以按如下方式拆分批次(视情况而定或取决于系统资源):

我使用CodeIgniter,当一个
insert\u批处理
不能完全工作时(插入的项目数与给定的项目数不同),我必须再次进行插入,使用insert ignore最大化通过过程的数量,而不会对现有项目产生错误。 当我使用这种方法时,我插入的数据类型不需要严格遵守给定的项数和放入数据库的项数。最大化是方法

a)尽可能多地使用
insert_batch
b)在失败时,使用变通方法,同时最大限度地减少不必要的请求,正确的方法是什么


感谢@q81提到的,您可以按如下方式拆分批次(视情况而定或取决于系统资源):

$insert_batch = array();
$maximum_items = 100;

$i = 1;
while ($condition == true) {
    // code to add data into $insert_batch
    // ...

    // insert the batch every n items
    if ($i == $maximum_items) {
        $this->db->insert_batch('table', $insert_batch); // insert the batch
        $insert_batch = array(); // empty batch array
        $i = 0;
    }
    $i++;
}

// the last $insert_batch
if ($insert_batch) {
    $this->db->insert_batch('table', $insert_batch);
}
编辑:


插入批处理时,插入的项目数与给定的项目数不同的原因可能是已达到允许的内存大小。这种情况发生在我身上的次数太多了。

使用
insert\u batch
插入数据的正确方法是:

CI_控制器:

public function add_monthly_record()
{
    $date               = $this->input->post('date');
    $due_date           = $this->input->post('due_date');
    $billing_date       = $this->input->post('billing_date');
    $total_area         = $this->input->post('total_area');
    $comp_id            = $this->input->post('comp_id');
    $unit_id            = $this->input->post('unit_id');
    $percent            = $this->input->post('percent');
    $unit_consumed      = $this->input->post('unit_consumed');
    $per_unit           = $this->input->post('per_unit');
    $actual_amount      = $this->input->post('actual_amount');
    $subsidies_from_itb = $this->input->post('subsidies_from_itb');
    $subsidies          = $this->input->post('subsidies');

    $data = array();
    foreach ($unit_id as $id => $name) {
      $data[] = array(
                    'date'               => $date,
                    'comp_id'            => $comp_id,
                    'due_date'           => $due_date,
                    'billing_date'       => $billing_date,
                    'total_area'         => $total_area,
                    'unit_id'            => $unit_id[$id],
                    'percent'            =>$percent[$id],
                    'unit_consumed'      => $unit_consumed[$id],
                    'per_unit'           => $per_unit[$id],
                    'actual_amount'      => $actual_amount[$id],
                    'subsidies_from_itb' => $subsidies_from_itb[$id],
                    'subsidies'          => $subsidies[$id],
                ); 
    };


    $result = $this->Companies_records->add_monthly_record($data);

    //return from model
    $total_affected_rows = $result[1];
    $first_insert_id = $result[0];

   //using last id 
    if ($total_affected_rows) {
        $count = $total_affected_rows - 1;
        for ($x = 0; $x <= $count; $x++) {
            $id = $first_insert_id + $x;
            $invoice = 'EBR' . date('m') . '/' . date('y') . '/' . str_pad($id, 6, '0', STR_PAD_LEFT);
            $field = array(
                'invoice_no' => $invoice,
            );
            $this->Companies_records->add_monthly_record_update($field,$id);
        }
    }
    echo json_encode($result); 
}

您可以拆分传递给insert_batch的数组,并根据拆分结果多次调用它。您可能知道完成了多少查询,基于此您可以决定拆分的大小。我正在考虑扩展insert\U batch以生成一个“max\U insert\U batch”函数,该函数的作用与insert\U batch相同,添加以下内容:每次调用_insert\U batch时,如果$this->impacted\u rows()!=$batch\u size(cf insert\u batch),如果$batch\u size>1,则使用相同的数据块递归调用它,batch\u size=floor(batch\u size/2),最终使用“忽略”选项。这将允许“在需要时重试”,同时避免在不需要时重试。你的意见?imho-你应该把你尝试过的代码发布出来,因为在文章中讨论这个主题有点问题theory@sintakonte:这是一个理论问题,我目前正在考虑一种最大化流程效率的方法,我现在没有写任何代码。复制一些随机代码,希望它永远在这里,不符合aswer的资格…我不认为这样拆分有什么意义:insert\U batch已经这样做了(或者我没有完全理解您的答案)。
public function add_monthly_record($data)
{
    $this->db->insert_batch('monthly_record', $data);
     $first_insert_id = $this->db->insert_id();
     $total_affected_rows = $this->db->affected_rows();
    return [$first_insert_id, $total_affected_rows];
}