Quantcast
Channel: Question and Answer » wpdb
Viewing all articles
Browse latest Browse all 44

Processing a lot of $wpdb isn't insert all the data

$
0
0

In my plugin the user provides some CSV files that are processed and either inserted or updated based on a sku (which acts as a foreign key to my DB and a primary key for where the CSV comes from).

I process each line and either insert product info or updated based on if the sku is available. There are 5503 products total. I have two tables one to hold the product id and sku and another to hold the details for that product.

when I run my function I check to see if the sku has a match. If not, I insert into the tables, if it does, I update. The first time I run, obviously everything should be an insert. I’ve done some debugging and found that insert is run every time, meaning there’s not an error causing updated to run.

I’ve ran print_r on the prod_details insert and I get all 5503 sets of arrays so nothing is being skipped. The products table is correct but for some reason the prod_details table doesn’t get all the inserts, only about 4122 are inserted of the 5503. I’m not sure if I’m dealing with a timeout, maybe a synchronous issue.

I’ve compared the skus between the two tables and found large chunks missing.

$prodTable = $wpdb->prefix.'products';
$prodDetails = $wpdb->prefix.'prod_details';

$skuId = $wpdb->get_var("SELECT id FROM $prodTable WHERE sku=$sku");

if($skuId) {
    //update the product table
    $wpdb->update(
    $prodTable,
    array(
        'status' => $status
    ),
    array(
        'id' => $skuId
    )
    );

    $wpdb->update(
    $prodDetails,
    array(
        'prodName' => $prodName,
        'typeName' => $prodType,
        'siteTypeName' => $siteType,
        'subTypeName' => $prodSubtype,
        'prodProducer' => $prodProducer,
        'styleName' => $prodStyle,
        'supplierName' => $supplier,
        'prodVintage' => $prodVintage
    ),
    array(
        'sku' => $sku
    )
    );

} else {
    $wpdb->insert(
    $prodTable,
    array(
        'sku' => $sku,
        'status' => $status
    )
    );
    $wpdb->insert(
    $prodDetails,
    array(
        'sku' => $sku,
        'prodName' => $prodName,
        'typeName' => $prodType,
        'siteTypeName' => $siteType,
        'subTypeName' => $prodSubtype,
        'prodProducer' => $prodProducer,
        'styleName' => $prodStyle,
        'supplierName' => $supplier,
        'prodVintage' => $prodVintage
    )
    );
}

I understand there’s some normalization I could probably do here but that would explain the insert issue. The above loops for each line of the CSV file (5503 times).

My local database inserts everything fine.


Viewing all articles
Browse latest Browse all 44

Trending Articles