在PHP中使用file_get_contents读取CSV

时间:2010-02-22 11:25:47

标签: php mysql

我正在阅读'种类'的csv文件并将其展开并将其存储在数组中。

我正在阅读的文件具有此结构

Id,Log,Res,File

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

现在我要做的是: 读取我的数据库中的最后一条记录,获取名称,在这种情况下让我们说'Test1',然后我搜索我的文件,在那里我可以找到'Test1'的位置并获取文件中的下一行,提取ID,s并将其添加到数据库。

我在文件中获得了所需字符串的位置,但我不知道如何获得下一行。

到目前为止,这是我的代码。

<?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 个答案:

答案 0 :(得分:0)

希望我能正确理解你。如果是这样,你可以在找到项目后设置一个标志,然后在每个行上爆炸,然后在'字符上。然后,结果数组中的第二个值应该包含您的示例中的id。

$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
    }


}

答案 1 :(得分:0)

如果id应该是唯一的,那么php进程和MySQL服务器之间的流量不是 限制因素,你必须解析每一行,你可以{{ 3}}让MySQL决定是否接受记录。

简单示例:

$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;
}

打印

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