2

I found and followed the directions contained within this StackOverflow thread: Update MySql Table from CSV using PHP

I've got an error somewhere that I'm unable to detect, I think there's a problem with my query, which works fine in actual MySQL but seems to not quite translate to PHP.

In short, I'm trying to UPDATE the value of several rows within a single table (catalog_product_entity_varchar) with CSV column $data[1], but only where certain skus are concerned AND attribute_id = 523 AND entity_id matches $data[0] of my CSV. Here's my code (actual PW/username, etc, obviously removed)

$con=mysqli_connect("localhost","username","password","some_db");

    if (!$con){
        die('Could not connect: ' . mysql_error());
    }

    if (($file = fopen("upload.csv", "r")) !== FALSE) {

        while (($data = fgetcsv($file)) !== FALSE) {

            $sql = "UPDATE catalog_product_entity_varchar 
                            JOIN catalog_product_flat_1
                            ON catalog_product_flat_1.entity_id = catalog_product_entity_varchar.entity_id 
                            SET catalog_product_entity_varchar.value='{$data[1]}' 
                            WHERE catalog_product_entity_varchar.entity_id='{$data[0]}'
                            AND catalog_product_entity_varchar.attribute_id = 523
                            AND (catalog_product_flat_1.sku LIKE  '%PR%'
                                 OR catalog_product_flat_1.sku LIKE  '%PT%'
                                 OR catalog_product_flat_1.sku LIKE  '%PF%')";

            if (mysql_query($con,$sql)) {
                echo "Updated!";
            } else {
                echo "Error updating " . mysql_error();
            }
        }
    }
    fclose($file);

It simply returns "Error updating" for every line of the spreadsheet. This query, when simply done in MySQL (without the PHP) and modified to have actual values instead of $data[1] or $data[0] works just fine. What am I missing?

If you're unclear of what I'm trying to achieve, I did post this question yesterday (trying to do it via pure mySQL) and there's more context here - https://stackoverflow.com/questions/21170245/updating-a-joined-table-in-mysql-from-a-csv

1 Answer 1

2

Wow.

So I feel stupid. Apparently mixing mysqli_connect and mysql_query doesn't work very well. Adding the "i" to the "mysql" of mysql_query solved it. Thanks for looking everyone!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.