0

In Magento2 I am trying to connect to an external database and table and use the data to update a custom product attribute where the sku' match in both tables. I temporarily put this code in my footer.phtml file as I am just trying. ti do a quick import/population of custom attributes from an external source

I am trying to do the below query. If I remove my query code and just card code a sku and a part id it updates Magento just fine so something about my sql it does not like. I confirmed my credentials are valid for accessing the database

$machine = "xxxxx";
$machineu = "xxxx";
$machinep = "xxx";
$machinedbname = "xxxx";

// Create connection
$conn = mysqli_connect($machine, $machineu, $machinep, $machinedbname);
if (!$conn) {
   die("Connection failed: " . mysqli_connect_error());
}

$testsql = mysqli_query($conn, "SELECT * FROM tempinfo");
while($row = mysqli_fetch_array($testsql))
{
  extract($row);
  echo "<p style='color:#ffffff'>test $sku</p><br />";
  $_product = $objectManager->create('\Magento\Catalog\Api\ProductRepositoryInterface')->get($sku,true, 0, true);
  $_product->setCustomAttribute("part_id", "$part_id");
  $_product->save($_product);
}

1 Answer 1

0

I am also using an external database to update some product infos, but I use PDO.

Maybe this helps:

     $sku=$_product->getSku();
     try {
             $conn = new \PDO("mysql:host=$servername;dbname=$dbname;port=$port", $username, $password);

             $selection="SELECT * from table WHERE id='$sku';
             foreach ($conn->query($selection) as $dbitem) {
               $_product->setCustomAttribute("part_id", $dbitem['part_id']);
               $_product->save();
             }
      } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
      }
2
  • Thank you but no matter what I do my query does not seem to work in Magento. Commented Nov 9, 2020 at 21:28
  • hmm...do you get any errors? Can you check, if there are any datasets in your table (maybe just echo "true" in the while-part)? are you sure, your credentials are correct and the table-name is correct? Commented Nov 9, 2020 at 21:36

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.