search
Homephp教程php手册PHP: 如何把数组导入MySQL数据库

PHP: 如何把数组导入MySQL数据库

Jun 06, 2016 pm 07:50 PM
mysqlphphowimportperformancedatabasearray

考虑到性能当然不能按数组元素遍历来执行插入操作。 有两个方法可供选择,一种是把数据存入csv文件,然后执行load data infile 还有一种就是类似于sql server里面的bulk insert,使用insert语句插入批量数据,结合PHP的implode函数, 可以很简单的实现大批量

考虑到性能当然不能按数组元素遍历来执行插入操作。

有两个方法可供选择,一种是把数据存入csv文件,然后执行load data infile

还有一种就是类似于sql server里面的bulk insert,使用insert语句插入批量数据,结合PHP的implode函数,

可以很简单的实现大批量数组数据的一次性插入。


$statement = "INSERT INTO table (title, type, customer) VALUES ";
foreach( $data as $row) {
   $statement .= ' ("' . implode($row, '","') . '")';
}

不过大批量数据的插入,需要注意mysql在内存上有限制:

http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_bulk_insert_buffer_size

bulk_insert_buffer_size变量的默认大小为8M, 是指单个线程占用的大小限制,设置为0,表示不做限制。


参考链接:

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html



by iefreer

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software