我有一个包含多个信息的文件文本,我想读取该文件 并剪切里面的数据:
我的文件如下所示:
firstname;lastname;computer;ip
firstname;lastname;computer;ip
firstname;lastname;computer;ip
我想在每次添加一行时在我的 mysql 库中加载该文件。 我从这个脚本开始,但我不明白为什么名字没有添加到我的数据库中 这是我的脚本:
<?php
$data = fopen("./session_name", "r");
$i = 0;
while ($line = fgets($data))
{
$i++;
$line = explode(';', $line);
$two = explode(';', $line[0]);
foreach($two as $t)
{
$insert = "INSERT INTO `session_test`(`prenom`, `nom`, `computer`, `adress_ip`) VALUES ('" . trim($t) . "',"","","")";
mysql_query($insert_data);
}
}
?>
最佳答案
<?php
$data = fopen("./session_name", "r");
$query = "INSERT INTO `session_test`(`prenom`, `nom`, `computer`, `adress_ip`) VALUES ";
while ($line = fgets($data)) {
$exploded = explode(';', $line);
$values[] = vsprintf("('%s','%s','%s','%s')",array_map('trim',$exploded));
}
$query .= implode(',',$values);
// mysqli or PDO code to query;
关于php 文件文本,每次在 mysql 基值数据中添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238900/