【问题标题】:php file text with added a line every time in mysql base value dataphp 文件文本,每次在 mysql 基值数据中添加一行
【发布时间】:2017-07-21 13:26:20
【问题描述】:

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

我的文件如下所示:

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);
    }
}
?>

【问题讨论】:

  • 首先,添加调试,看看你的代码是否有问题。 mysql_query($insert_data) 或死(mysql_error());另外, print_r($two) 以查看您的数组是否按预期创建。
  • $line[0] 不应该有任何;s,因为您之前的爆炸将它们全部删除。 $line[0] 应该是 firstname
  • ,不要对新代码使用mysql_* 函数。它们不再被维护,社区已经开始 deprecation processmysql_* 函数已在 PHP 7 中正式删除。相反,您应该了解 prepared statements 并使用 PDOmysqli_*。如果你不能决定,this article will help to choose your best option.

标签: php mysql file text line


【解决方案1】:
<?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;

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 2020-09-13
    • 1970-01-01
    • 2018-03-01
    • 2023-02-07
    • 1970-01-01
    • 2017-11-28
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多