2012-06-21 64 views
0

我创建了一个php脚本,我将把它放到cron作业中,并且应该在午夜执行,但这不是问题。问题是当cron运行我的代码时,它删除了数据库中的所有记录,但不会从CSV文件中插入新记录。任何提示?将csv导入到mysql数据库 - 错误

PHP代码如下:

<?php 

# I run this script from a cron job every night to update 
# the mysql database I use with my employee web site 
# so it matches my local database every day. Feel free to 
# modify it to meet your specific needs. If you find it 
# usefull, drop me an email and let me know. 

# edit the follow six items to use the script 

# first connect to your mysql database 
# i have my connection settings in a diferent file 
# so i just include that file in all my scripts 
include("db.php"); 

# assign the tables that you want to import to to the table array 
$table = array( 
       'table1', 
       'table2', 
       'table3', 
       'table4', 
       'table5', 
     ); 

# if the first row of your csv file contains column headings: 
# $columnheadings=1 
# if the first row does not contain column headings and should be imported: 
# $columnheadings=0 
$columnheadings = 0; 

# contains the email address you want the results sent to 
$emailaddress = "[email protected]"; 

# contains the subject you want the message to have 
$subject = "Enter Subject Here"; 

# contains the email address that will show in the from line 
$emailfrom = "[email protected]"; 

# perform the required operations for every table listed in the table array 
foreach ($table as $tablename) { 

    # empty the table of its current records 
    $deleterecords = "TRUNCATE TABLE `$tablename`"; 
    mysql_query($deleterecords); 

    # intialize your counters for successful and failed record imports 
    $pass = 0; 
    $fail = 0; 

    # the csv file needs to be the same name as the table, 
    # comma seperated with the columns in the same order as the table, 
    # and in the same dir as this script 
    $filecontents = file ("$tablename.csv"); # .csv is added to the table name to get the name of the csv file 

    # every record in the csv file will be inserted into the table unless an error occurs with that record 
    for($i=$columnheadings; $i<sizeof($filecontents); $i++) { 
     $insertrecord = "Insert Into `$tablename` Values ($filecontents[$i])"; 
     mysql_query($insertrecord); 
     if(mysql_error()) { 
      $fail += 1;  # increments if there was an error importing the record 
     } 
     else 
     { 
      $pass += 1;  # increments if the record was successfully imported 
     } 
    } 

    # adds a line to the email message we will send stating how many records were imported 
    # and how many records failed for each table 
    $message .= "Table $tablename: Success=$pass Failure=$fail \n"; 
} 

# set to the date and time the script was run 
$runtime = (date("d M Y H:i")); 

# add the run time to the body of the email message 
$message .= "\nTime of the message: $runtime (server time zone)\n\n"; 

# Send the email message 
mail($emailaddress, $subject, $message, "From: '$emailfrom'"); 

?> 
+0

CSV文件与此脚本位于同一目录中吗?如果不是,则需要提供CSV文件的完整路径。此外,如果文件无法打开,我会添加一个错误条件。 – EmmanuelG

+0

对于调试,您应至少记录可能发生的错误之一。 – Sirko

+0

是的,csv是在同一个目录下,当我直接从浏览器运行文件时没有错误只是空白页 – Eager2Learn

回答

0

请告诉我表的模式,而你在最后得到的$message什么?你是否获得所有行失败?

补充:

由于所有行失败,意味着在INSERT语句的一些问题,或者可能是CSV和目标表之间的架构不匹配。试试这个

# and in the same dir as this script 
    $filecontents = file ("tablename.csv"); # .csv is added to the table name to get the name of the csv file 

    # every record in the csv file will be inserted into the table unless an error occurs with that record 
    for($i=$columnheadings; $i<sizeof($filecontents); $i++) { 
     $insertrecord = "Insert Into `$tablename` Values ($filecontents[$i])"; 
     mysql_query($insertrecord); 
     if(mysql_error()) { 
      $fail += 1;  # increments if there was an error importing the record 
      //this is include exact error in email, and will help you to identify the 
      //problem very easily. 
      $message .= mysql_errno() . ": " . mysql_error() . "\n"; 
     } 
     else 
     { 
      $pass += 1;  # increments if the record was successfully imported 
     } 
    } 

试试这个,让我知道你的发现?

+0

我根本没有收到任何错误。只是所有的数据都从tabel中删除 – Eager2Learn

+0

你在电子邮件中收到了什么?在你发送电子邮件的脚本结尾,它会让你知道有多少行通过,有多少次失败? –

+0

我不接收任何来自文件的消息,只删除部分作品;( – Eager2Learn

1

你的代码没有检查文件的存在 - 那么,如果它不在你想要的位置呢?根据你如何从cron运行它,它可能从不同的目录运行,如果你从网上运行它。

+0

所有文件自动上传到webhost上的正确目录 – Eager2Learn

+0

但是,没有检查你的代码是看在文件夹中,因为你的代码没有检查它的内部文件夹,如果从cron运行,它可能从你的主目录运行,而不是web目录。你也可以从web上得到一个空白页面,因为除了邮件之外,这可能由于某种原因失败...为什么不写一个文件检查案件..甚至屏幕作为一个检查。(cron通常默认邮寄任何输出给用户) – BugFinder

+0

好的,但无论如何,我的脚本有错误becouse不向mysql表插入数据,这是一个很大的探针 – Eager2Learn