Tag Archives: Query

Magento 2 Custom Query

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();

$sql = "Select * FROM customer_entity" ;
$result = $connection->fetchAll($sql); 
print_r($result);
?>

How to write a Custom SQL Query Magento

To select the data

$read = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);;
$selectQry = “SELECT * FROM catalog_category_entity”;
$write->query($selectQry);

To add/modify data

$write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$updateQry = “UPDATE catalog_category_entity_varchar SET value=’three_columns’ WHERE attribute_id=’61’ “;
$write->query($updateQry);

Magento Custom Query

To select the data

$read = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);;
$selectQry = “SELECT * FROM catalog_category_entity”;
$write->query($selectQry);

To add/modify data

$write = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
$updateQry = “UPDATE catalog_category_entity_varchar SET value=’three_columns’ WHERE attribute_id=’61’ “;
$write->query($updateQry);