Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to insert multiple rows into mysql by a single query using codeigniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 8.16k
    Comment on it

    Most of time we need to insert multiple rows at a time into the database but some developer use multiple INSERT statements to insert multiple rows while it is a bad approach to solve this problem. Codeigniter provides a insert batch function.

    Example:- 
    $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); or $sql = array(); foreach( $data as $row ) { $sql[] = '("'.mysql_real_escape_string($row['text']).'", '.$row['category_id'].')'; } $this->db->query('INSERT INTO table (text, category) VALUES '.implode(',', $sql));

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: