Guest User

Untitled

a guest
Apr 13th, 2021
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1.  public  function insertManyRows(string $table, array $data)
  2.     {
  3.    
  4.         $columnsName = $this->getColumnName($table);
  5.  
  6.  
  7.         $columns = array_column($columnsName, 'COLUMN_NAME');
  8.         $columnKeys   = array_flip($columns);
  9.         $insertString = function ($dataRow) use ($columnKeys) {
  10.            return array_intersect_key($dataRow, $columnKeys);
  11.         };
  12.  
  13.         $inserts = array_map($insertString, $data);
  14.         $keys=array_keys($inserts[0]);
  15.         $i=0;
  16.  
  17.         foreach ($inserts as $value){
  18.             if($i==0){
  19.                 $values="('" . implode("', '", $value)  . "')";
  20.             }else{
  21.                 $values=$values.",('" . implode("', '", $value)  . "')";
  22.             }
  23.             $i+=1;
  24.         }
  25.      
  26.         $fieldNames   = implode(', ', $keys);
  27.         $sql = "INSERT INTO `$table` ($fieldNames) VALUES :values ";
  28.  
  29.         //TODO log
  30.         return $this->exec($sql,array('values'=>$values),true);
  31.  
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment