Magento 2 Update mass Product description using $mysqli->real_escape_string() using core php root script
-
15-04-2021 - |
Question
After migrating all data from Magento 1.9 aproximat 800 product have static image urls in descriptin.
{{skin url="images/seringue.gif"}}
So I have created one root script to update all product descriptions programmatically...
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
use Magento\Framework\App\Bootstrap;
include 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/product_update_description.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Update Product Description'); // Simple Text Log
$logger->info('ID'); // Simple Text Log
$allProductIds = [7,8,9,10,11,12,13,14,15];
foreach ($allProductIds as $productId){
$product = $objectManager->create('Magento\Catalog\Model\Product');
$product->load($productId);
$description = str_replace('{{skin url="images/seringue.gif"}}','{{media url="wysiwyg/seringue.gif"}}',$product->getDescription());
$product->setDescription($description);
$logger->info($product->getId().','.$product->getSku()); // Simple Text Log
$product->save();
}
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$selectSql = "SELECT * FROM `catalog_product_entity_text` WHERE `value` LIKE '%{{skin url=\"images/seringue.gif\"}}%' AND value_id='37531'";
$result = $connection->fetchAll($selectSql);
$resource->closeConnection();
foreach ($result as $product) {
//print_r($product['value']);
$logger->info($product['entity_id']);
$description = str_replace('{{skin url="images/seringue.gif"}}','{{media url="wysiwyg/seringue.gif"}}',$product['value']);
$sql = "UPDATE `catalog_product_entity_text` SET `value` = '".$description."' WHERE `value_id` = '".$product['value_id']."';";
$connection->query($sql);
}
The full script runs without success, in the database, nothing is changed.
No correct solution
OTHER TIPS
when I have manually updated the description in the database and compare both queries then I found that this is an issue of HTML data are encrypted and in MySQL query that was run in PHPMyAdmin and this is an issue in so I have used the mysql_real_escape_string
function but the letter I found that this function is deprecated in PHP 5 and removed from php 7 :D.
$sql = "UPDATE `catalog_product_entity_text` SET `value` = '".mysql_real_escape_string ($description)."' WHERE `value_id` = '".$product['value_id']."';";
So I have run bellow script and all product description are updated successfully.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);
$url='localhost:3306';
$username='magento2_db';
$password='pass';
$db = "mage_v241";
$conn=mysqli_connect($url,$username,$password,$db);
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
$mysqli = new mysqli($url,$username,$password,$db);
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$result = mysqli_query($conn,"SELECT * FROM `catalog_product_entity_text` WHERE `value` LIKE '%{{skin url=\"images/seringue.gif\"}}%'");
echo "entity id";
while($row = mysqli_fetch_array($result)) {
echo "<br>";
echo $row['entity_id'];
$description = str_replace('{{skin url="images/seringue.gif"}}','{{media url="wysiwyg/seringue.gif"}}',$row['value']);
$description = $mysqli -> real_escape_string($description);
$sql = "UPDATE `catalog_product_entity_text` SET `value` = '".$description."' WHERE `value_id` = '".$row['value_id']."';";
mysqli_query($conn,$sql);
}
?>
Thank you.