Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpdb 'load data file'

I'm using the wpdb class to run a LOAD DATA LOCAL INFILE. It works fine and the data is inserted properly - but it still returns 0, so echos 'No Update' in the example. Is this the expected outcome based on the LOAD DATA method? If so, is there some other way of knowing that the process ran and did indeed insert rows?

Thanks much!

Philip

global $wpdb;
$filename = 'file.csv';
$sql = "LOAD DATA LOCAL INFILE '" . $filename . "'
INTO TABLE Stock_Item
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 ROWS
(stock_item_code, stock_item_name)"; 

$result = $wpdb->query($sql);           
if ($result === false) { echo 'Query Fail'; }
if ($result === 0) { echo 'No update'; }
if ($result > 0) { echo 'Success'; }
like image 860
Philip Light Avatar asked Sep 03 '26 01:09

Philip Light


1 Answers

i am doing this by this way.

global $wpdb;
        $datafile= $_FILES['file']['tmp_name'];
        $file=$upload_dir['basedir'].'/'.$_FILES['file']['name'];
        $fileurl=$upload_dir['baseurl'].'/'.$_FILES['file']['name'];
        if (!move_uploaded_file(
        $_FILES['file']['tmp_name'],
        $file)) {
        print_r('Failed to move uploaded file.');
        }
        $sql="
        LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE ".CRSSEARCH_TABLE."
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY '\r\n'
        (ucn,name, course_number, standard, accredition,attended_course_form,attended_course_to,status,validity,location);
        ";
        $query = $wpdb->query($sql);

and work fine for me like charm.