fork download
  1. <?php
  2.  
  3. class Database {
  4.  
  5. private $user = 'root';
  6. private $pass = '';
  7. private $dbName = 'test_data';
  8.  
  9. public function __construct() {
  10. $this->db = new PDO('mysql:host=localhost;dbname=' . $this->dbName, $this->user, $this->pass);
  11. }
  12.  
  13. function insertBatch($table, $data) {
  14.  
  15. $tableColNamesArray = array();
  16. $tableColParamsArray = array();
  17. $tableColValuesArray = array();
  18.  
  19.  
  20. foreach ($data as $num => $dataRow) {
  21.  
  22. $oneRowValuesArray = array();
  23.  
  24. foreach ($dataRow as $tableColName => $value) {
  25. if ($num == 1) {
  26. $tableColNamesArray[] = $tableColName;
  27. $tableColParamsArray[] = '?';
  28. }
  29. $oneRowValuesArray[] = $value;
  30.  
  31. }
  32. $tableColValuesArray[] = $oneRowValuesArray;
  33. }
  34.  
  35. $query = 'INSERT INTO '. $table .' (' . implode(', ', $tableColNamesArray) . ') VALUES (' . implode(', ', $tableColParamsArray) . ')';
  36.  
  37. // var_dump($tableColValuesArray);
  38. // exit;
  39.  
  40. $this->db->beginTransaction();
  41. $stmt = $this->db->prepare($query);
  42.  
  43. foreach($tableColValuesArray as $insertRow) {
  44. //var_dump($insertRow);
  45.  
  46. $stmt->execute($insertRow);
  47. }
  48. $this->db->commit();
  49. print_r($this->db->errorInfo());
  50. }
  51.  
  52. }
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
Standard output is empty