function ReadData($x1,$date)
{
$strPathName = "H:/pp/ThaiCom/TH/" ;
$strFile = $strPathName.$x1 ;
//$ckdate=explode("_",$x1);
//$ccdate=GregorianToJD($ckdate1[0],$ckdate1[1],$ckdate1[2]);
//$t[0]=$ckdate[0];
echo"file is ".$strFile."</br>" ;
if(file_exists($strFile)){
//echo "Have file <br/>";
$objFopen = fopen($strFile, 'r');
while (!feof($objFopen))
{
$file = fgets($objFopen);
$TMsel=trim($file);
$compare = substr($TMsel,0,5);
$value = trim(substr($TMsel,strpos($TMsel," ")));
$t[17]="";
$t[0]=$date;
switch ($compare)
{
case "T007K": echo $compare.$value."<br>"; $t["1"] = $value; break;
Case "T008K": echo $compare.$value."<br>"; $t["2"] = $value; break;
Case "T009K": echo $compare.$value."<br>"; $t["3"] = $value; break;
Case "T010K": echo $compare.$value."<br>"; $t["4"] = $value; break;
Case "T011K": echo $compare.$value."<br>"; $t["5"] = $value; break;
Case "T012K": echo $compare.$value."<br>"; $t["6"] = $value; break;
Case "T013K": echo $compare.$value."<br>"; $t["7"] = $value; break;
Case "T014K": echo $compare.$value."<br>"; $t["8"] = $value; break;
Case "T020K": echo $compare.$value."<br>"; $t["9"] = $value; break;
Case "T021K": echo $compare.$value."<br>"; $t["10"] = $value; break;
Case "T022K": echo $compare.$value."<br>"; $t["11"] = $value; break;
Case "T023K": echo $compare.$value."<br>"; $t["12"] = $value; break;
Case "T024K": echo $compare.$value."<br>"; $t["13"] = $value; break;
Case "T056K": echo $compare.$value."<br>"; $t["14"] = $value; break;
Case "T057K": echo $compare.$value."<br>"; $t["15"] = $value; break;
Case "T058K": echo $compare.$value."<br>"; $t["16"] = $value; break;
}
//echo "file Exist";
}
fclose($objFopen);
return $t;
}
else{
exit;
}
}
$objConnect = odbc_connect("mydatabase","","") or die("Error Connect to Database");
$strSQL = "SELECT Max(Time) as LastestTime FROM Thermal ";
$objExec = odbc_exec($objConnect, $strSQL) or die ("Error Execute [".$strSQL."]");
while(odbc_fetch_row($objExec))
{
$LastRecord = odbc_result($objExec,"LastestTime") ;
}
$year = substr($LastRecord,0,4);
$month = substr($LastRecord,5,2);
$day = substr($LastRecord,8,2);
$hour= substr($LastRecord,-8,2)+1;
$today=date("d/m/t");
$LDate=mktime(10,0,0,1,1,2009);
$y=date("d/m/Y H:i",$LDate);
echo "max date is ".$y;
echo "today is ".$today."</br>";
//echo $hour;
odbc_close($objConnect);
for($m=$month;$m <= 12; $m++){
for ($d=$day;$d <= 31; $d++){
for($h=$hour;$h<=23;$h++){
if (strlen($m)== 1){$m ="0".$m ;}
if (strlen($d)== 1){$d ="0".$d ;}
if (strlen($h)== 1){$h ="0".$h ;}
$StampD = $d."/".$m."/".$year." ".$h.":00";
if ($StampD>$y) break;
echo "stampdate = ".$StampD;
$nextRecord = $year.$m.$d."_" . $h."_00.txt" ;
echo"next record is ".$nextRecord."</br>";
$Data=" ";
$Data= ReadData($nextRecord,$StampD) ;
echo"result is </br>" ;
foreach($Data as $NRecord){
print $NRecord."</br>";
//echo "<hr color=pink>";
// $connect = odbc_connect($dsn, $user, $pass) or die("ติดต่อ DSN ไม่ได้"); // เริ่มติดต่อฐานข้อมูล
// $sql = "select * from book"; // กำหนดคำสั่ง SQL เพื่อแสดงข้อมูล
// $exec = odbc_exec($connect, $sql); // เริ่มเอ็กซิคิวต์คำสั่ง SQL
// $num_fields = odbc_num_fields($exec);
$objConnect2 = odbc_connect("mydatabase","","") or die("Error Connect to Database");
$strSQL2 = "INSERT into Thermal values(".$NRecord.")";
$objExec2 = odbc_exec($objConnect, $strSQL) or die ("Error Execute [".$strSQL."]");
odbc_close($objConnect2);
}
}$hour=0;
}$day=1;
} $month=1;
$year=$year+1;
echo"Finish";
//odbc_close($objConnect);
//odbc_close($objConnect2);
เมื่อลองรันปรากฏว่าขึ้น error แบบนี้
Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] The table 'Thermal' is already opened exclusively by another user, or it is already open through the user interface and cannot be manipulated programmatically., SQL state S1000 in SQLExecDirect in H:\Myproject\RunTH_ori.php on line 55
Error Execute [SELECT Max(Time) as LastestTime FROM Thermal ]
ลองแล้วคะ แต่ก็ขึ้น error ว่า Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement., SQL state 37000 in SQLExecDirect in H:\Myproject\RunTH_ori.php on line 111
Error Execute [INSERT into Thermal(Time,T007K,T008K,T009K,T010K,T011K,T012K,T013K,T014K,T020K,T021K,T022K,T023K,T024K,T056K,T057K,T058K) values('01/01/2009 02:00','12.22','11.85','12.22','10.74','11.85','12.59','12.96','12.22','45.50','49.50','47.50','47.00','47.00','10.37','10.74','42.50')]