Reading a CSV with file_get_contents in PHP
        Posted  
        
            by JPro
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by JPro
        
        
        
        Published on 2010-02-22T11:25:47Z
        Indexed on 
            2010/06/15
            13:02 UTC
        
        
        Read the original article
        Hit count: 432
        
I am reading a 'kind' of csv file and exploding it and storing it in array.
The file I am reading has this structure
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.
I am getting the position of desired string in the file, but I am not sure how to get the next lines too.
Here's my code so far.
<?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>";
    }
    }
© Stack Overflow or respective owner