【发布时间】: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 process 和mysql_*函数已在 PHP 7 中正式删除。相反,您应该了解 prepared statements 并使用PDO或mysqli_*。如果你不能决定,this article will help to choose your best option.