2010-02-22 205 views
0

我正在读取一种'类'csv文件并将其分解并将其存储在数组中。在PHP中使用file_get_contents读取一个CSV

的文件我读了这个结构

Id,Log,Res,File 

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

现在我所要做的是: 读我的数据库中的最后一条记录,得到了名字,让我们在这种情况下说“测试1”和然后我通过我的文件进行搜索,在那里我可以找到'Test1'的位置并获取文件中的下一行,提取ID,并将其添加到数据库。

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

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

<?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>"; 


    } 

    } 

回答

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 
    } 


} 
0

如果id应该是唯一的,PHP程序和MySQL服务器之间的流量不限制因素,你总得去分析每一行,你可以create a unique index,让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