使用分隔符将数据从文件插入到表中,然后将其放入数据库

时间:2022-09-25 21:10:37

I have a text file in which it is written in this style there data:
Floran / Marielle
I would like to retrieve separated by ; and then store them in a database (SQL Server)
Facts for example table contains firstname (varchar) and name (varchar) I do not know if I have to store this information in a variable or table or what to do first?

我有一个文本文件,在这个样式中写有数据:Floran / Marielle我想检索分隔;然后将它们存储在数据库(SQL Server)中事实例如表包含firstname(varchar)和name(varchar)我不知道是否必须将此信息存储在变量或表中或先做什么?

1 个解决方案

#1


1  

// read file into $data
$data = file_get_contents('names.txt');

// create an array by splitting at every /
$data = explode('/',$data);

// create an SQL statement to insert into a database
$max = count($data);
$sql = '';

for ($i = 0; $i < $max; $i+=2)
    $sql .= '("' . trim($data[$i]) . '","' . trim($data[$i+1]) . '"),';

$sql = 'INSERT INTO tablename (firstname,name) VALUES ' .trim($sql,',');

// add code for inserting into your db

#1


1  

// read file into $data
$data = file_get_contents('names.txt');

// create an array by splitting at every /
$data = explode('/',$data);

// create an SQL statement to insert into a database
$max = count($data);
$sql = '';

for ($i = 0; $i < $max; $i+=2)
    $sql .= '("' . trim($data[$i]) . '","' . trim($data[$i+1]) . '"),';

$sql = 'INSERT INTO tablename (firstname,name) VALUES ' .trim($sql,',');

// add code for inserting into your db