PHP file_get_contents ile bir CSV Okuma

2 Cevap php

Ben csv dosyasının bir 'tür' okuma ve patlayan ve dizide saklayarak duyuyorum.

Ben okuyorum dosya, bu yapıya sahiptir

Id,Log,Res,File

mydb('Test1','log1','Pass','lo1.txt').
mydb('Test2','log2','Pass','lo2.txt').
mydb('Test3','log3','Pass','lo3.txt').

Now what I am trying to do is : reading the last record in my database, get the Name, lets say in this case 'Test1' and then I am searching through my file and where I can find the position of 'Test1' and get the next lines in the file, extract the ID,s and add it to database.

Ben dosyasında istenilen dize pozisyonunu alıyorum, ama ben de bir sonraki satırları almak için nasıl emin değilim.

İşte benim kod çok uzak.

<?php


    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("testing") or die(mysql_error());



$result = mysql_query("select ID from table_1 order by  S_no DESC limit 1") or die(mysql_error());  
$row = mysql_fetch_array( $result );
$a = $row['ID'];
echo 'Present Top Row is '.$a.'<br>';

$addresses = explode("\n", file_get_contents('\\\\fil1\\logs\\tes.pl'));

    foreach($addresses as $val)
    {

    $pos = strstr($val, $a);

    if ($pos === false) {

    } else {
        echo "The string <br> '$a' <br>was found in the string <br>'$val' <br>";
        echo " and exists at position <br>$pos<br>";


    }

    }

2 Cevap

Ben doğru anlamak umuyoruz. Öyleyse öğe bulunduğunda kez sadece bir bayrak ayarlamak ve daha sonra bir 'karakteri sonra her satırı patlayabilir. Sonuçta dizideki ikinci değer daha sonra örnek başına kimliği içermelidir.

$addresses = explode("\n", file_get_contents('\\\\fil1\\logs\\tes.pl'));

$bFound = false;
foreach($addresses as $val)
{

    if (!$bFound) {
      $pos = strstr($val, $a);

      if ($pos === false) {          

      } else {
          echo "The string <br> '$a' <br>was found in the string <br>'$val' <br>";
          echo " and exists at position <br>$pos<br>";
          $bFound = true;
      }
    }
    else {
      $aCols = explode("'", $val);
      $sId = $aCols[1];
      // Now add Id as stored in $sId to DB here
    }


}

id benzersiz olması gerekiyordu ise, php süreci ve MySQL sunucusu arasındaki trafiği the Eğer {[(2, sınırlayıcı faktör olabilir ve yine her satır ayrıştırmak zorunda değil )]} ve MySQL bu kayıt ya da değil kabul edip karar verelim.

Basit bir örnek:

$pdo = new PDO('mysql:host=localhost;dbname=test', 'localonly', 'localonly'); 
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$pdo->exec('CREATE TEMPORARY TABLE foo (Id varchar(16),Log varchar(16),Res varchar(16),File varchar(16), unique key(Id))');
$stmt = $pdo->prepare('INSERT IGNORE INTO foo (Id,Log,Res,File) VALUES(?,?,?,?)');
$pattern = '/(?<=^mydb\().+(?=\)\.$)/';
$fp = 'dummy';
while ( false!==($row=dummy_fgets($fp)) ) {
  if ( preg_match($pattern, $row, $m) ) {
    $row = str_getcsv($m[0], ',', "'");
    $stmt->execute($row);
    echo $row[0], ' -> ', $stmt->rowCount(), "\n";
  }
}

function dummy_fgets($dummy) {
  static $data = array(
    "mydb('Test1','log1','Pass','lo1.txt').\n",
    "mydb('Test2','log2','Pass','lo2.txt').\n",
    "mydb('Test3','log3','Pass','lo3.txt').\n",
    // start again ...like you process the same file again + 1 new record
    "mydb('Test1','log1','Pass','lo1.txt').\n",
    "mydb('Test2','log2','Pass','lo2.txt').\n",
    "mydb('Test3','log3','Pass','lo3.txt').\n",
    "mydb('Test4','log3','Pass','lo3.txt').\n"
  );
  $rv = current($data);
  next($data);
  return $rv;
}

baskılar

Test1 -> 1
Test2 -> 1
Test3 -> 1
Test1 -> 0
Test2 -> 0
Test3 -> 0
Test4 -> 1