Jump to content

fgetcsv()


learnphp1

Recommended Posts

Hi

 

I am uploading a csv file in the datbase. and i want to show an error message if the columns are more than 5.

 

I am counting the rows in the below script but it always prints row 1. I have spent lot of time to figure out and google it. all the examples in google looks similar to mine.

 

Can anyone find any issue with the below script and correct me pls

 

Thanks in advance

 

$row=1;
        $error = 'No'; //////////initialise the error flag
        
        while (($data = fgetcsv($handle, 1000, ",",'"')) !== FALSE) {
            if( count($data) > 5 ){
                    //print_r($data);
                    $error = 'Yes' ; ///////////set the error flag to yes and break the loop . validation failed
                    $errorMess = 'There are more than 5 columns in row # '.$row ;
                    break ;
            }
        ///////////////insert the data into stagging table if the column ==5///////////////////
        //print_r($data);
          $sqlLoad = "INSERT INTO  ratecodes_stagging ( package_name,package_table,plan_name,plan_code,package_hierachy  )
                      VALUES ('".addslashes($data[0])."','".addslashes($data[1])."','".addslashes($data[2])."','".addslashes($data[3])."',
                      '".addslashes($data[4])."')";
          $resultSqlLoad = es_query($sqlLoad);
        $row++;
        //echo $i."<br>";
        }//END while fgetcsv 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.