PDO Prepared Inserts multiple rows in single query

后端 未结 22 2539
感情败类
感情败类 2020-11-21 23:38

I am currently using this type of SQL on MySQL to insert multiple rows of values in one single query:

INSERT INTO `tbl` (`key1`,`key2`) VALUES (\'r1v1\',\'r1         


        
22条回答
  •  暖寄归人
    2020-11-21 23:52

    A shorter answer: flatten the array of data ordered by columns then

    //$array = array( '1','2','3','4','5', '1','2','3','4','5');
    $arCount = count($array);
    $rCount = ($arCount  ? $arCount - 1 : 0);
    $criteria = sprintf("(?,?,?,?,?)%s", str_repeat(",(?,?,?,?,?)", $rCount));
    $sql = "INSERT INTO table(c1,c2,c3,c4,c5) VALUES$criteria";
    

    When inserting a 1,000 or so records you don't want to have to loop through every record to insert them when all you need is a count of the values.

提交回复
热议问题