溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

mysql中怎么使用php批量插入數(shù)據(jù)

發(fā)布時(shí)間:2021-07-24 16:16:31 來源:億速云 閱讀:226 作者:Leah 欄目:數(shù)據(jù)庫

這篇文章將為大家詳細(xì)講解有關(guān)mysql中怎么使用php批量插入數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

  假如說我有這樣一個(gè)表,我想往這個(gè)表里面插入大量數(shù)據(jù):

  CREATE TABLE IF NOT EXISTS `user_info` (

  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',

  `name` varchar(255) NOT NULL default '' COMMENT '姓名',

  `age` int(11) NOT NULL default '0' COMMENT '年齡',

  PRIMARY KEY (`id`)

  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶信息表';

  批量插入

  方法一、使用for循環(huán)插入

  在往mysql插入少量數(shù)據(jù)的時(shí)候,我們一般用for循環(huán):

  $arr = [

  [

  'name' => 'testname1',

  'age' => 18,

  ],

  [

  'name' => 'testname2',

  'age' => 19,

  ],

  [

  'name' => 'testname3',

  'age' => 18,

  ],

  ];

  $servername = "localhost";

  $port = 3306;

  $username = "username";

  $password = "password";

  $dbname = "mytestdb";

  // 創(chuàng)建連接

  $conn = new mysqli($servername, $username, $password, $dbname, $port);

  // 檢測連接

  if ($conn->connect_error) {

  die("connect failed: " . $conn->connect_error);

  }

  $costBegin = microtime(true);

  foreach($arr as $item) {

  $sql = sprintf("INSERT INTO user_info (name, age) VALUES ( '%s', %d);", $item['name'], (int)$item['age']);

  if ($conn->query($sql) === TRUE) {

  echo "insert success";

  } else {

  echo "Error: " . $sql . "< br>" . $conn->error;

  }

  }

  $costEnd = microtime(true);

  $cost = round($costEnd - $costBegin, 3);

  var_dump($cost);

  $conn->close();

  假如說要批量插入大量數(shù)據(jù),如果還用for循環(huán)的辦法插入是沒有問題的,只是時(shí)間會(huì)比較長。對比一下插入少量數(shù)據(jù)與插入大量數(shù)據(jù),使用上面的for循環(huán)插入耗費(fèi)的時(shí)間:條數(shù)時(shí)間(單位:秒)。

  方法二、使用insert語句合并插入

  mysql里面是可以使用insert語句進(jìn)行合并插入的,比如:

  INSERT INTO user_info (name, age) VALUES ('name1', 18), ('name2', 19);表示一次插入兩條數(shù)據(jù)

  $arr = [

  [

  'name' => 'testname1',

  'age' => 18,

  ],

  [

  'name' => 'testname2',

  'age' => 19,

  ],

  [

  'name' => 'testname3',

  'age' => 18,

  ],

  // 此處省略

  ……

  ……

  ];

  $servername = "localhost";

  $port = 3306;

  $username = "username";

  $password = "password";

  $dbname = "mytestdb";

  // 創(chuàng)建連接

  $conn = new mysqli($servername, $username, $password, $dbname, $port);

  // 檢測連接

  if ($conn->connect_error) {

  die("connect failed: " . $conn->connect_error);

  }

  $costBegin = microtime(true);

  if (!empty($arr)) {

  $sql = sprintf("INSERT INTO user_info (name, age) VALUES ");

  foreach($arr as $item) {

  $itemStr = '( ';

  $itemStr .= sprintf("'%s', %d", $item['name'], (int)$item['age']);

  $itemStr .= '),';

  $sql .= $itemStr;

  }

  // 去除最后一個(gè)逗號,并且加上結(jié)束分號

  $sql = rtrim($sql, ',');

  $sql .= ';';

  if ($conn->query($sql) === TRUE) {

  } else {

  echo "Error: " . $sql . "

  " . $conn->error;

  }

  }

  $costEnd = microtime(true);

  $cost = round($costEnd - $costBegin, 3);

  var_dump($cost);

  $conn->close();

  下面看一下少量數(shù)據(jù)與大量數(shù)據(jù)的時(shí)間對比。從總體時(shí)間上,可以看出insert合并插入比剛才for循環(huán)插入節(jié)約了很多時(shí)間,效果很明顯條數(shù)時(shí)間(單位:秒)。

  如果你覺得數(shù)組太大,想要減少sql錯(cuò)誤的風(fēng)險(xiǎn),也可以使用array_chunk將數(shù)組切成指定大小的塊,然后對每個(gè)塊進(jìn)行insert合并插入。

關(guān)于mysql中怎么使用php批量插入數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI