insert multiple rows via a php array into mysql

后端 未结 12 1513
自闭症患者
自闭症患者 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条回答
  •  -上瘾入骨i
    2020-11-21 23:26

    You could prepare the query for inserting one row using the mysqli_stmt class, and then iterate over the array of data. Something like:

    $stmt =  $db->stmt_init();
    $stmt->prepare("INSERT INTO mytbl (fld1, fld2, fld3, fld4) VALUES(?, ?, ?, ?)");
    foreach($myarray as $row)
    {
        $stmt->bind_param('idsb', $row['fld1'], $row['fld2'], $row['fld3'], $row['fld4']);
        $stmt->execute();
    }
    $stmt->close();
    

    Where 'idsb' are the types of the data you're binding (int, double, string, blob).

提交回复
热议问题