简体   繁体   English

PHP 逐行读取文件,声明为变量,然后在 MySQL 查询中作为 WHERE column_name 等于变量使用

[英]PHP read from file line by line, declare as variable, then use in MySQL query as WHERE column_name equals variable

Good day, My aim with the script (overview) is to let PHP read from text file line by line then declare the line as variable that will be used in a MySQL statement as a where clause into an array, then ultimately writing to another text file.美好的一天,我的脚本(概述)的目标是让 PHP 逐行读取文本文件,然后将该行声明为变量,该行将在 MySQL 语句中用作数组中的 where 子句,然后最终写入另一个文本文件。

CODE:代码:

$lines = file('/root/prcode');
foreach($lines as $line) {
        $query = "select * from $db_table where code=$line";
        $result = mysqli_query($conn,$query);
        while($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
        $out = "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7],
$row[8],$row[9],$row[10],$row[11],$row[12],\n";

        //file_put_contents($outfile, $out);
        //$fp = fopen($outfile, 'w');
        //fwrite($fp, $out);
        //fclose($fp);
echo $out;
//echo $line;
//echo $query;
//echo $result;
//echo $row;
};
}

File prcode's first 10 lines:文件 prcode 的前 10 行:

60025909170
*
.05005140160
0000000000000000
0000000000000001
0000000000000004
0000000000000005
0000000000000007
0000000000000010
0000000000000011

OUTPUT when script is run from Linux shell:从 Linux shell 运行脚本时的输出:

0010,0000,60025909170,01,,,,,,,,,,
0010,0000,60025909170,02,,,,,,,,,,
0010,0000,60025909170,03,,,,,,,,,,
0010,0000,60025909170,04,,,,,,,,,,
0010,0000,60025909170,05,,,,,,,,,,
0010,0000,60025909170,06,,,,,,,,,,
0010,0000,60025909170,07,4.000,36.960,-4.000,-36.960,,,,,,
0010,0000,60025909170,08,4.000,36.960,,,,,,,,
0010,0000,60025909170,09,4.000,36.960,,,,,,,,
0010,0000,60025909170,10,4.000,36.960,,,,,,,,
0010,0000,60025909170,11,4.000,36.960,,,,,,,,
0010,0000,0000060025909170,01,,,,,,,,,,
0010,0000,0000060025909170,02,,,,,,,,,,
0010,0000,0000060025909170,03,,,,,,,,,,
0010,0000,0000060025909170,04,,,,,,,,,,
0010,0000,0000060025909170,05,,,,,,,,,,
0010,0000,0000060025909170,06,,,,,,,,,,
0010,0000,0000060025909170,07,,,,,,,,,,
0010,0000,0000060025909170,08,,,,,,,,,,
0010,0000,0000060025909170,09,,,,,,,,,,
0010,0000,0000060025909170,10,-4.000,-.040,4.000,.040,,,,,,
0010,0000,0000060025909170,11,-4.000,-.040,,,,,,,,
0010,0000,60025909170,12,4.000,36.960,,,,,,,,
0010,0000,0000060025909170,12,-4.000,-.040,,,,,,,,

From the output, it can be established that the order of the text file is not being followed as 60025909170 and 0000060025909170 are 2 different products and 0000060025909170 is at about line 32000 <- my first problem.从输出中,可以确定没有遵循文本文件的顺序,因为600259091700000060025909170是两种不同的产品,而0000060025909170大约在第 32000 行 < - 我的第一个问题。 To try to rectify this and cater for following the order, the special characters and spaces I tried:为了尝试纠正这一点并满足以下顺序,我尝试了特殊字符和空格:

$query = "select * from $db_table where code='$line'";  \\causes empty output
$query = "select * from $db_table where code=\"$line\""; \\causes empty output
$query = "select * from $db_table where code=\'$line\'"; \\empty output

The echos below were just to test to see which variables are set or not to try to further troubleshoot:下面的回声只是为了测试看看设置了哪些变量,或者不尝试进一步排除故障:

echo $out;
//echo $line;
//echo $query;
//echo $result;
//echo $row;

The second problem is, none of the attempts to write to file worked, it would either write just a single line and nothing more or would not write at all.第二个问题是,写入文件的尝试都没有奏效,它要么只写一行,什么也不写,要么根本不写。

Any advise or guidance on how to possibly fix my 2 issues?关于如何解决我的两个问题的任何建议或指导?

1st issue: Likely, your file is being read in order, but your query is finding all of those rows.第一个问题:很可能,您的文件正在按顺序读取,但您的查询正在查找所有这些行。 You can see if this is happening by adding the line number to your output.您可以通过将行号添加到输出中来查看是否发生了这种情况。 Try changing your foreach to foreach($lines as $lineNum => $line) { and your output to $out = $lineNum.implode(',', $row)."\\n";尝试将您的 foreach 更改为foreach($lines as $lineNum => $line) {并将您的输出更改$out = $lineNum.implode(',', $row)."\\n"; If you have the same lineNum for 60025909170 and 0000060025909170, then you know that the query is matching both.如果 60025909170 和 0000060025909170 的 lineNum 相同,那么您就知道查询匹配两者。

2nd issue: You just need to add a flag of FILE_APPEND to the file_put_contents.第二个问题:您只需要在 file_put_contents 中添加一个 FILE_APPEND 标志。 Like this: file_put_contents($outfile, $out, FILE_APPEND) ;像这样: file_put_contents($outfile, $out, FILE_APPEND) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM