欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

利用php在mysql中插入大量數(shù)據(jù)的方法

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

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站制作、灌南網(wǎng)絡(luò)推廣、成都微信小程序、灌南網(wǎng)絡(luò)營銷、灌南企業(yè)策劃、灌南品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供灌南建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:chinadenli.net

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ì)比較長。對(duì)比一下插入少量數(shù)據(jù)與插入大量數(shù)據(jù),使用上面的for循環(huán)插入耗費(fèi)的時(shí)間:條數(shù)時(shí)間(單位:秒)

利用php在mysql中插入大量數(shù)據(jù)的方法

方法二、使用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è)逗號(hào),并且加上結(jié)束分號(hào)
$sql = rtrim($sql, ',');
$sql .= ';';

if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}

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

$conn->close();

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

利用php在mysql中插入大量數(shù)據(jù)的方法

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

以上就是php在mysql里批量插入數(shù)據(jù)(代碼實(shí)例)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!

文章名稱:利用php在mysql中插入大量數(shù)據(jù)的方法
轉(zhuǎn)載注明:http://chinadenli.net/article28/gjedjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)ChatGPT、App設(shè)計(jì)、小程序開發(fā)網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營