insert multiple rows via a php array into mysql

后端 未结 12 1669
自闭症患者
自闭症患者 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条回答
  •  Happy的楠姐
    2020-11-21 23:18

    Well, you don't want to execute 1000 query calls, but doing this is fine:

    $stmt= array( 'array of statements' );
    $query= 'INSERT INTO yourtable (col1,col2,col3) VALUES ';
    foreach( $stmt AS $k => $v ) {
      $query.= '(' .$v. ')'; // NOTE: you'll have to change to suit
      if ( $k !== sizeof($stmt)-1 ) $query.= ', ';
    }
    $r= mysql_query($query);
    

    Depending on your data source, populating the array might be as easy as opening a file and dumping the contents into an array via file().

提交回复
热议问题