Home > Backend Development > PHP Tutorial > The fgetcsv function in php imports csv files into mysql database_PHP tutorial

The fgetcsv function in php imports csv files into mysql database_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 16:57:25
Original
830 people have browsed it

This article will give you a detailed introduction to the fgetcsv function to import csv files into the mysql database. At the same time, the solution to Chinese garbled characters can be referred to.


When processing large batches of excel data to the mysql database, use the fgetcsv function that comes with PHP to first obtain the data line by line from the csv file, and then import it into the database with the sql statement. If you encounter garbled characters, you can use the iconv function to transcode.


Code example:

} }

The code is as follows
 代码如下 复制代码

$row = 1;
$handle = fopen("test.csv","r");
while ($data = fgetcsv($handle, 1000, ",")) {//1000为csv文件总行数,逗号是数据分隔符,这两个参数均可以忽略不写
$num = count($data);
echo "

第 $row行有$num个字段。
n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "
n";
}
}
fclose($handle);
?>

Copy code

$row = 1;

$handle = fopen("test.csv","r");

while ($data = fgetcsv($handle, 1000, ",")) {//1000 is the total number of lines in the csv file, and the comma is the data separator. Both parameters can be ignored

$num = count($data);

echo "

Row $row has $num fields.
n";

 代码如下 复制代码

setlocale(LC_ALL, 'zh_CN.UTF8′);

$row++;

for ($c=0; $c < $num; $c++) {

echo $data[$c] . "
n";
fclose($handle); ?>

Chinese garbled characters when importing mysql When using common online methods to read and upload the content of a csv file into the mysql database, Chinese characters are displayed as garbled characters. Even after transcoding the data using the iconv function, the characters are still garbled. In this case, you can use the setlocale() function: This function is used to configure regional information. Before reading and writing csv data, use this function to define it. For example, if my csv file is in UTF-8 format without BOM, use the following function first. Definition:
The code is as follows Copy code
setlocale(LC_ALL, 'zh_CN.UTF8′);
Then use the iconv function to transcode the data content into the database and other subsequent operations. http://www.bkjia.com/PHPjc/631522.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631522.htmlTechArticleThis article will give you a detailed introduction to the fgetcsv function to import the csv file into the mysql database. At the same time, in the Chinese garbled Friends can refer to the solution. Processing large quantities...
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template