我想从我的 php 中的 csv 文件中提取登录详细信息.我的代码只接受 csv 文件中的最后一个值


I want to extract login details from a csv file in my php. My code only accepts the last values in the csv file

我想从我的php中的csv文件中提取登录详细信息。我的代码只接受 csv 文件中的最后一个值。这是我的代码。

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $username= $_POST['name'];
        $email= $_POST['email'];
        $file_handle = fopen("passwords.csv", "r");
        while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle);
        print $line_of_text[0] . $line_of_text[1]. "<BR>";
        }
        if ($username === $line_of_text[0] && $email === $line_of_text[1] ){
            echo "yes";
        } else{
            echo "no";
        }

可能您正在寻找的答案是这样的:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $username= $_POST['name'];
    $email= $_POST['email'];
    $file_handle = fopen("passwords.csv", "r");
    $found = FALSE;
    while (!feof($file_handle) ) {
        $line_of_text = fgetcsv($file_handle);
        print $line_of_text[0] . $line_of_text[1]. "<BR>";
        if ($username === $line_of_text[0] && $email === $line_of_text[1] )
        {
            $found = TRUE;
        }
    }
    if ($found){
        echo "yes";
    } else{
        echo "no";
    }

尝试:

while (! feof($file_handle)) {
    print_r(fgetcsv($file_handle));
}