脚本读取行,从值中选择,并打印到文件

问题描述:

所以我有一个PHP脚本,我想逐行阅读文件,每行只包含一个ID。我想为文件中的每个ID选择使用sql,然后在同一个文件中打印每个选择的结果。脚本读取行,从值中选择,并打印到文件

到目前为止,我有:

while (!feof($file)) 
{ 
    // Get the current line that the file is reading 
    $currentLine = fgets($file) ; 
    //explodes integers by amount of sequential spaces 
    //$currentLine = preg_split('/[\s,]+/', $currentLine); 
    echo $currentLine; //this echo statement prints each line correctly 
    selectQuery($currentLine) ; 


} 

fclose($file) ; 

作为测试到目前为止我只有

function selectQuery($currentLine){ 
    echo $currentLine; //this is undefined? 
} 
+0

除非有一个在'$ currentLine'的在selectQuery呼叫名字,我看不出这些代码会怎么失败的呼应实际功能里面有些东西看不见/不可打印字符。 –

+0

@ user570098:这是所有相关的代码吗?如果你可以在字符串之后回显字符串,那么你可以使用它作为你的函数的参数,它应该在IMO工作。此外,它看起来像你要做每行选择查询,也许最好考虑'缓存'你想查询的信息,只执行一个查询。 – PeeHaa

fgets的结果绝不会是不确定的。但是,你的方法太低级了。使用filearray_filter

$results = array_filter(file('input.filename'), function(line) { 
    return strpos($line, '4') !== false; // Add filter here 
}); 
var_export($results); // Do something with the results here