2016-02-19 126 views
4

我试图从csv文件中提取值提取到一个MySQL表。它运行但不填充表格。我试图调试过去的XXXX,但只是看不到我的错误。回应值给我正确的SQL,但是当涉及到INSERT时 - 没有骰子。从csv文件插入值到MySQL表

非常感谢您的帮助。

<?php 

$host = 'localhost'; 
$user = 'fulltime_admin'; 
$pass = 'secret'; 
$database = 'fulltime_db'; 

$db = mysql_connect($host, $user, $pass); 
mysql_query($database, $db); 

//////////////////////////////// EDIT //////////////////////////////////// 

$redirect_num = 500; // Select how many rows to insert each time before refresh. 
// More rows = faster insertion. However cannot be too high otherwise it will timeout. 

$filename = "ps4_emails.csv"; // The file we are going to get the data from... 

$table = "`ps4_emails`"; 

////////////////////////////// END EDIT ////////////////////////////////// 

$file = file($filename); 
$lines = count($file); 

// Have we just redirected? 
$nextline = $_GET['nextline']; 
if (!isset($nextline)){ 
    $nextline = 0; 
} 

$query = "INSERT INTO ".$table." (email) VALUES ('".$final_line[0]."')"; 

for ($line=$nextline; $line<=$lines; $line++){ 

    $final_line = explode(",", $file[$line]); 

    if ($line!=$lines){ 
     mysql_query($query,$db); 

    } 

    if ($line % $redirect_num){ 

     // something needs to go here 
    } else { 
     $nextline = $line+1; 
     exit ('<meta http-equiv="refresh" content="0;url=texttomysqlemails.php?nextline='.$nextline.'" />'); 
    } 

    echo ($line==$lines) ? "Done" : ""; 

} 
?> 
+1

请勿使用mysql _ \ *函数。改为使用mysqli _ \ *或PDO与准备好的语句。 –

+1

并更改您的数据库密码。 – ThiefMaster

+1

那么你期望这个脚本运行多长时间?你有多少行加载到你的数据库 – RiggsFolly

回答

1

把你的查询中循环,从而具有可变$final_line使用它。

试试这个:

$final_line = explode(",", $file[$line]); 

if ($line!=$lines){ 
    $query = "INSERT INTO ".$table." (email) VALUES ('".$final_line[0]."')"; 
    mysql_query($query,$db); 
} 

不要使用mysql_*。它已被弃用并从PHP 7中删除。使用mysqli_*PDO

+0

嗯......还是不行 – baselinej70

+0

我是否需要定义$ final_line作为数组? – baselinej70

+0

不,你不需要这样做PHP是松散类型的 –

1

这似乎是从命令行PHP CLI运行的完美脚本,因此您可以忽略所有刷新复杂性。

如果文件很大,就像您的注释所显示的那样,将所有文件加载到内存中也可能会使您遇到PHP内存限制,所以最好一次读取一行而不是整个文件用于读取csv文件的fgetcsv()

<?php 

    $host = 'localhost'; 
    $user = 'fulltime_admin'; 
    $pass = 'secret'; 
    $database = 'fulltime_db'; 

    $db = mysql_connect($host, $user, $pass); 
    mysql_query($database, $db); 

    $filename = "ps4_emails.csv"; 
    $table = ""; 

    $handle = fopen('ps4_emails.csv', 'r'); 
    if (! $handle) { 
     echo 'File does not exists in this location'; 
     exit; 
    } 

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 

     $query = "INSERT INTO `ps4_emails` (email) VALUES('{$data[0]}')";   

     mysql_query($query,$db); 
    } 
?> 

您现在只需运行命令行/终端这个剧本就像

>php script.php 

而且它可以为分钟/小时/天,没有likleyhood吹任何限制的运行。

我不得不提这样或有人会唠叨我不是说这

请不要使用mysql_数据库扩展,它已被弃用(在PHP7一去不复返) 特别是如果你刚开始学习PHP ,花你的精力学习PDOmysqli_数据库扩展, and here is some help to decide which to use

当你需要上传真实的文件,它也将是添加一个重启机制是一个好主意,S o您可以从发生问题的任何时候重新启动进程,或者有人关闭数据库以备份或其他未预见的呃逆。

<?php 

    $host = 'localhost'; 
    $user = 'fulltime_admin'; 
    $pass = 'secret'; 
    $database = 'fulltime_db'; 

    $restart_from = 0; 

    $db = mysql_connect($host, $user, $pass); 
    mysql_query($database, $db); 

    $filename = "ps4_emails.csv"; 
    $table = ""; 

    $handle = fopen('ps4_emails.csv', 'r'); 
    if (! $handle) { 
     echo 'File does not exists in this location'; 
     exit; 
    } 

    // is it a restart? 
    if (file_exists('restart.txt')) { 
     // its a restart 
     $restart_from = file_get_contents('restart.txt'); 

     // read up the file to the last good row inserted 
     for ($i=0; $i<=$restart_from; $i++) { 
      $data = fget($handle, 1000); 
     } 
    } 

    $upd_cnt = restart_from; 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 

     $query = "INSERT INTO `ps4_emails` (email) VALUES('{$data[0]}')";   

     mysql_query($query,$db); 

     $upd_cnt++; 
     file_put_contents('restart.txt', $upd_cnt); 
    } 
?> 

上述重启代码没有进行测试,但我已经用过的东西非常喜欢这个在过去非常成功。所以你必须检查我没犯过什么愚蠢的错误,但它应该让你知道如何在崩溃前成功更新的最后一行重新启动。