英文:
How to export multiply data from a .txt and insert into a database
问题
I can provide a translation of your text as requested:
如何从不同的文本文件中导出多个数据并将数据导入数据库?
所以我有一个名为:admin_cars 的数据库和一个表格:brands;如下所示:
CREATE TABLE `brands` (
`id` int(11) NOT NULL,
`bmw` varchar(500) NOT NULL,
`mercedes` varchar(500) NOT NULL,
`audi` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
现在我想从:/var/www/html/bmw/index.txt 导出数据到 -> bmw。
我已经在 .PHP 中编写了脚本,它运行正常。以下是代码:
<?php
$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo '连接失败: ' . $e->getMessage();
}
$file = new SplFileObject('/var/www/html/bmw/index.txt');
while (!$file->eof()) {
$line = $file->fgets();
list($bmw) = explode(';', $line);
$sth = $dbh->prepare('INSERT INTO brands values(NULL,?)');
$sth->bindValue(1, $bmw, PDO::PARAM_STR);
$sth->execute();
}
?>
但现在我还想从 /var/www/html/mercedes/index.txt 导出数据到 -> mercedes,以及从 /var/www/html/audi/index.txt 导出数据到 -> audi。
我尝试了以下代码:
<?php
$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo '连接失败: ' . $e->getMessage();
}
$file = new SplFileObject('/var/www/html/bmw/index.txt');
while (!$file->eof())
$file = new SplFileObject('/var/www/html/mercedes/index.txt');
while (!$file->eof())
$file = new SplFileObject('/var/www/html/audi/index.txt');
while (!$file->eof()) {
$line = $file->fgets();
list($bmw) = explode(';', $line);
$line = $file->fgets();
list($mercedes) = explode(';', $line);
$line = $file->fgets();
list($audi) = explode(';', $line);
$sth = $dbh->prepare('INSERT INTO brands values(NULL,?,?,?)');
$sth->bindValue(1, $bmw, PDO::PARAM_STR);
$sth->bindValue(2, $mercedes, PDO::PARAM_STR);
$sth->bindValue(3, $audi, PDO::PARAM_STR);
$sth->execute();
}
?>
但当我运行这个 .PHP 脚本时,我收到错误消息并出现故障。什么是将多个数据导出到数据库表的最佳解决方案?
非常感谢您的时间和帮助!
英文:
How can i export multiply data from different text files and import the data into a database?
So i have a database called: admin_cars and a table: brands ; like:
CREATE TABLE `brands` (
`id` int(11) NOT NULL,
`bmw` varchar(500) NOT NULL,
`mercedes` varchar(500) NOT NULL
`audi` varchar(500) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Now i want to export the data from: /var/www/html/bmw/index.txt into -> bmw
I have already written the script in .PHP and it works fine. This is the code:
<?php
$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$file= new SplFileObject('/var/www/html/bmw/index.txt');
while(!$file->eof())
{
$line=$file->fgets();
list($bmw)=explode(';',$line);
$sth = $dbh->prepare('INSERT INTO brands values(NULL,?)');
$sth->bindValue(1,$bmw,PDO::PARAM_STR);
$sth->execute();
}
?>
But now i want to export also the data from /var/www/html/mercedes/index.txt into -> mercedes and /var/www/html/audi/index.txt into -> audi
I tried this:
<?php
$dsn = 'mysql:dbname=admin_cars;host=127.0.0.1';
$user = 'admin_cars';
$password = '********';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$file= new SplFileObject('/var/www/html/bmw/index.txt');
while(!$file->eof())
$file= new SplFileObject('/var/www/html/mercedes/index.txt');
while(!$file->eof())
$file= new SplFileObject('/var/www/html/audi/index.txt');
while(!$file->eof())
{
$line=$file->fgets();
list($bmw)=explode(';',$line);
$line=$file->fgets();
list($mercedes)=explode(';',$line);
$line=$file->fgets();
list($audi)=explode(';',$line);
$sth = $dbh->prepare('INSERT INTO brands values(NULL,?,?,?)');
$sth->bindValue(1,$bmw,PDO::PARAM_STR);
$sth->bindValue(2,$mercedes,PDO::PARAM_STR);
$sth->bindValue(3,$audi,PDO::PARAM_STR);
$sth->execute();
}
?>
But i get a error message and faults when i run the .PHP script.
What is the best solution to export multiply data into the tables of the database?
Thank you very much for your time and help!
答案1
得分: 0
You're using same variable for all files data repeatedly, so maybe that is an issue. For start, try reading them separately and write them to DB with one while loop after another.
您多次重复使用相同的变量来处理所有文件数据,这可能是一个问题。首先尝试分别读取它们,然后使用一个while循环写入数据库。
$file_bmw= new SplFileObject('/var/www/html/bmw/index.txt');
while(!$file_bmw->eof())
{
// 输入到数据库
}
$file_mercedes= new SplFileObject('/var/www/html/mercedes/index.txt');
while(!$file_mercedes->eof())
{
// 输入到数据库
}
What is the error displayed ?
请问显示了什么错误?
英文:
You're using same variable for all files data repeatedly, so maybe that is an issue. For start, try reading them separately and write them to DB with one while loop after another.
$file_bmw= new SplFileObject('/var/www/html/bmw/index.txt');
while(!$file_bmw->eof())
{
// input to db
}
$file_mercedes= new SplFileObject('/var/www/html/mercedes/index.txt');
while(!$file_mercedes->eof())
{
// input to db
}
What is the error displayed ?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论