【发布时间】:2015-06-11 09:58:16
【问题描述】:
比较有问题。我在执行nmap 并在数据库中保存pid 后执行此代码:
include("classes/database.php"); //conection to database
$selectActual = "SELECT * from InfActual";
$resultActual = $conn->query($selectActual);
if ($resultActual->num_rows > 0) {
while ($rowActual = $resultActual->fetch_assoc()) {
echo "pid in DB: " . $rowActual['pid'] . "<br>";
echo "................<br>";
exec("pgrep -l 'nmap' | cut -d' ' -f1 > pids.txt");
$fh = fopen('pids.txt', 'r');
while ($line = fgets($fh)) {
if ($line == $rowActual['pid']) {
echo "pid in pids.txt: " . $line . " and pid in DB: " . $rowActual['pid'] . " are the same <br>";
} else {
echo "pid in pids.txt: " . $line . " and pid in DB: " . $rowActual['pid'] . " are NOT the same <br>";
}
}
}
}
fclose($fh);
结果如下:
pid in DB: 5926
................
pid in pids.txt: 5926 and pid in DB: 5926 are NOT the same
为什么它告诉我不一样?我不明白。 提前谢谢你。
[已解决]
if (intval($line) == intval($rowActual['pid']))
【问题讨论】:
-
问题可能是因为您在文本文件的每一行末尾都有一个换行符 (
\n)。所以:"xy\n" === "xy"与您可能看到的不同。 (解决方案?只需trim()文件中的行,例如if (trim($line) == $rowActual['pid']))
标签: php if-statement compare