insert multiple rows via a php array into mysql

后端 未结 12 1519
自闭症患者
自闭症患者 2020-11-21 22:35

I\'m passing a large dataset into a MySQL table via PHP using insert commands and I\'m wondering if its possible to insert approximately 1000 rows at a time via a query othe

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 23:22

    You can do it with several ways in codeigniter e.g.

    First By loop

    foreach($myarray as $row)
    {
       $data = array("first"=>$row->first,"second"=>$row->sec);
       $this->db->insert('table_name',$data);
    }
    

    Second -- By insert batch

    $data = array(
           array(
              'first' => $myarray[0]['first'] ,
              'second' => $myarray[0]['sec'],
            ),
           array(
              'first' => $myarray[1]['first'] ,
              'second' => $myarray[1]['sec'],
            ),
        );
    
        $this->db->insert_batch('table_name', $data);
    

    Third way -- By multiple value pass

    $sql = array(); 
    foreach( $myarray as $row ) {
        $sql[] = '("'.mysql_real_escape_string($row['first']).'", '.$row['sec'].')';
    }
    mysql_query('INSERT INTO table (first, second) VALUES '.implode(',', $sql));
    

提交回复
热议问题