insert multiple rows via a php array into mysql

后端 未结 12 1520
自闭症患者
自闭症患者 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:31

    Although it is too late to answer this question. Here are my answer on the same.

    If you are using CodeIgniter then you can use inbuilt methods defined in query_builder class.

    $this->db->insert_batch()

    Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:

    $data = array(
        array(
                'title' => 'My title',
                'name' => 'My Name',
                'date' => 'My date'
        ),
        array(
                'title' => 'Another title',
                'name' => 'Another Name',
                'date' => 'Another date'
        )
    );
    
    $this->db->insert_batch('mytable', $data);
    // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'),  ('Another title', 'Another name', 'Another date')
    

    The first parameter will contain the table name, the second is an associative array of values.

    You can find more details about query_builder here

提交回复
热议问题