我想从.text和Database中找到相同的行,然后根据文本文件中的新行更新数据库

时间:2015-04-19 10:23:46

标签: php mysql database

我在文本文件中有一些数据,如。

02.01.2015;07:00;54;Normal;----;2
02.01.2015;07:00;80;Normal;----;2
02.01.2015;07:00;97;Normal;----;2
02.01.2015;07:05;48;Normal;----;2

我想将所有数据更新为数据库。但我在Dababase已有的数据。像

02.01.2015;07:00;97;Normal;----;2
02.01.2015;07:05;48;Normal;----;2

现在我想在数据库中已经有02.01.2015;07:05;48;Normal;----;2,因此无法更新。

只有这个和其他所有数据都可以更新到数据库。

$alldata = AllData($conn);
$row = 1;
if (($handle = fopen("0115.txt", "r")) !== FALSE){
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { 
    $num = count($data); 
    //echo "<p> $num fields in line $row: <br /></p>\n"; 
    $row++;
 // for ($c=0; $c < $num; $c++) { 
//  print_r($data); 
//  echo $data[$c].'<hr>';
//  } 

    $row1 = $data[0];
    $row2 = $data[1];
    $row3 = $data[2];
    $row4 = $data[3];
    $row5 = $data[4];
    $row6 = $data[5];

    $TextRows = $row1.$row2.$row3;

            if(!empty($alldata)){
            foreach($alldata as $alld){
                    $Datum = $alld['Datum'];
                    $Zeit = $alld['Zeit'];
                    $Pers_No = $alld['Pers_No'];
                    $DataBase = $Datum.$Zeit.$Pers_No;  
                    if($DataBase == $TextRows){
                        echo '';
                    }else{



          // update Query.  // when i print this code. so this code can't work showing some record like 
  //   02.01.2015;07:05;48;Normal;----;2 // 1.2.3 again many time.
                    }
            }

            }
    }
    fclose($handle); 
    } 
//  for ($c=0; $c < $num; $c++) { 
//  echo $splitcontents[$c]; 
//  } 

1 个答案:

答案 0 :(得分:0)

<?php
    $file = fopen("0115.txt","r");
    while(! feof($file))
    {
      $lineData =  fgets($file);//Each Line Data
      $dataArr = explode(",",$lineData);
      $checkData = "SELECT col1,col2,col3 FROM table_name WHERE col1='".$dataArr[0]."' AND col2='".$dataArr[1]."' AND col3='".$dataArr[2]."'";
      $dbData = mysql_query();
      $noOfRows = mysql_num_rows($dbData);//Check If data available in the DB.
      if($noOfRows == 0){
            //Write Your Query to Update your table.
      }
    }

    fclose($file);
?>