php 文件文本,每次在 mysql 基值数据中添加一行

标签 php mysql file text line

我有一个包含多个信息的文件文本,我想读取该文件 并剪切里面的数据:

我的文件如下所示:

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/

相关文章:

php - Laravel Faker 工厂关系

php - json在php中解码并插入到mysql DB中

mysql - 使用 MySQL 查找链接表中没有任何条目的项目

c++ - 从文件中读取和显示字符串

php - MySQL – 我应该使用 last_insert_id() 吗?或者是其他东西?

php - 使用 PHP filter_input_array 过滤多维 POST

php - 使用javascript获取php boolean 值

sql - Debian 服务器上的 Mysql 错误

python - 在 Python 中从文件加载参数

file - 在 Nginx 上配置缓存 css/js 文件