Import a list of CSV files into DB
Searched these code from Google.
Import a list of CSV files into their appropriate tables and email the results when finished.
By : Keegan
<?php
# Author: Keegan
# Email: keegan@sifizm.com
# Web Site: www.sifizm.com
# 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 = “user@domain.com”;
# 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 = “user@domain.com”;
# you should not have to edit anything below this line
# 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’”);
?>
Schedule PHP
看来的PHP定时执行代码段~
想法挺不错的,但是服务器受得了否.
回头测试一下.
<?php
ignore_user_abort(); // 即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set_time_limit(0); // 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
$interval=20; // 时间间隔 单位 秒
$key_file=”key.txt”; // 配置文件
if (isset($_GET['s']))
{
if ($_GET['s']==”0″){ // 停止工作,但不退出
$s=”false”;
echo “Function is off”;
}
elseif ($_GET['s']==”1″){ // 工作
$s=”true”;
echo “Function is on”;
}
elseif ($_GET['s']==”2″){ // 退出
$s=”die”;
echo “Function exited”;
}
else
die(“Err 0:stop working 1:working 2:exit”);
$string = “<?php\n return \”".$s.”\”;\n?>”;
write_inc($key_file,$string,true);
exit();
}
if(file_exists($key_file)){
do{
$mkey = include $key_file;
if ($mkey==”true”){ // 如果工作
///////////////////// 工作区间 //////////////////////////////////
$showtime=date(“Y-m-d H:i:s”);
$fp = fopen(‘func.txt’,'a’);
fwrite($fp,$showtime.”\n”);
fclose($fp);
///////////////////////////////////////////////////////////////////
}
elseif ($mkey==”die”){ // 如果退出
die(“I am dying!”);
}
sleep($interval); // 等待$interval分钟
}while(true);
}
else
die($key_file.” doesn’t exist !”);
function write_inc($path,$strings,$type=false)
{
$path=dirname(__FILE__).”/”.$path;
if ($type==false)
file_put_contents($path,$strings,FILE_APPEND);
else
file_put_contents($path,$strings);
}
?>
source http://blbear.com/post/187
Programmer’s life
<a href=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_7PWG_7303000071972d12.gif” title=”p_large_7PWG_7303000071972d12.gif” onclick=”return hs.expand(this)” target=”_blank”><img src=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_7PWG_7303000071972d12.gif” alt=”p_large_7PWG_7303000071972d12.gif” width=”160″ height=”160″ /></a><a href=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_bnRx_1fd800010c522d0e.gif” title=”p_large_bnRx_1fd800010c522d0e.gif” onclick=”return hs.expand(this)” target=”_blank”><img src=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_bnRx_1fd800010c522d0e.gif” alt=”p_large_bnRx_1fd800010c522d0e.gif” width=”160″ height=”160″ /></a><a href=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_CDl4_143300006f8c2d13.gif” title=”p_large_CDl4_143300006f8c2d13.gif” onclick=”return hs.expand(this)” target=”_blank”><img src=”http://blog.xrdavies.com/wp-content/uploads/2010/04/p_large_CDl4_143300006f8c2d13.gif” alt=”p_large_CDl4_143300006f8c2d13.gif” width=”160″ height=”160″ /></a>