1
global $wpdb;

if (isset($_POST["import"])) {

$fileName = $_FILES["file"]["tmp_name"];

if ($_FILES["file"]["size"] > 0) {

    $file = fopen($fileName, "r");

    while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {


        $table_name = 'sas' ;


        $wpdb->insert( 


            $table_name,


            array( 


            'category' => '" . $column[0] . "',

            'temple' => '" . $column[0] . "'

        )
            );





    }
}
}

I want to import csv directly into the core wordpress database table 'sas' .The csv contains some records with the structure of the table i have created...But the import is not successfull..

1 Answer 1

0

You can use Load data infile MySQL Query instead of looping through each entry.

Doc: https://dev.mysql.com/doc/refman/8.0/en/load-data.html

For example:

$wpdb->query(
                $wpdb->prepare(
                        "LOAD DATA LOCAL INFILE %s INTO TABLE sas FIELDS TERMINATED BY ',' ENCLOSED BY %s IGNORE 1 LINES (@category,@temple) SET category = @category, temple = @temple;", $_FILES['file']['tmp_name'], '"'
                )
        );

Make sure that fields from file are properly mapped to those in DB. Also it will change as per your file formatting. Hope this helps.

Note: Please check for syntax error/ typos, code is not tried or tested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.