简体   繁体   中英

PHP while loop not working

I can't understand why my while loop stops after the first row of data is inserted. Please help. I am trying to upload a .csv file in a MySql table - and if the data already exist, skip and go to the second row.

<?php
$dbhost = "xxxx";
$dbname = "xxxx";
$dbuser = "xxxx";
$dbpass = "xxxx";
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());

fgetcsv_PHP();

function fgetcsv_PHP()
{

    if (($handle = fopen("events.csv", "r")) !== FALSE)
    {

        $length = 1000;
        $delimiter = ",";



        while ( ( $data = fgetcsv( $handle) ) !== FALSE )
        {
            // Count number of array elements in $data
            $num = count($data);
            // Print opening table row HTML tag
            $data[5]= strtolower($data[5]);
            $data[11]= "2012-06-22 10:30:40";
            $import="INSERT into candidatedb(event,rec,title,name,surname,email,phone,discip,exper,wheread,comment,stamp) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]')";


            $result = mysql_query("SELECT * from candidatedb WHERE email = '$data[5]' AND surname = '$data[4]'");
            $num_rows = mysql_num_rows ($result);
            echo $num_rows;

            if ($num_rows > 0) {

                echo "Email address: $data[5] already exist </br>"; 
            }

            else {      

                    mysql_query($import) or die(mysql_error());
                    echo "Done! $data[5] inserted<br/>";
            }
        }
        // Close the file pointed to by $handle
        fclose($handle);
    }
}

"Done!<br/> $num record uploaded" 
?>




<?php /*?>
       $import="INSERT into candidatedb(event,rec,title,name,surname,email,phone,discip,exper,wheread,comment,stamp) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[9]','$data[10]','$data[11]')";

       mysql_query($import) or die(mysql_error());
<?php */?>

what ist the output if you try debugging with this:

fgetcsv_PHP();

function fgetcsv_PHP() {

$length    = 1000;
$delimiter = ",";
if (($handle = fopen("events.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, $length, $delimiter)) !== FALSE)
    {
        echo "read line: ".implode($data, ';')." <br>\n";
    }
    fclose($handle);
}
}

Ok, I found the problem. It was not the code it was the .csv file. For some reason the .csv version of Excel for Mac "comma separated values" isn't decoded properly by the php function. However if you export the .csv as "windows comma separated (.csv)" then it works like a charm.

在此处输入图片说明

Thank you for all your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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