Thursday, July 9, 2015

Magento direct SQL queries

Step 1: Initialize a resource object that will communicate with the database.

// To read from the database
$readObject = Mage::getSingleton( 'core/resource' )->getConnection( 'core_read' );

// To write to the database
$writeObject = Mage::getSingleton( 'core/resource' )->getConnection( 'core_write' );

Step 2: Set table name with it's prefix
$dbTable = Mage::getSingleton( 'core/resource' )->getTableName( 'custom_product' );

Step 3: Prepare query
$sql = "SELECT additional_description FROM " . $dbTable . " WHERE id:productId";
$filters = array(
'productId' => $productId
);

Step 4: Run your query.
$result = $readObject->query( $sql, $filters );
while ( $row = $result->fetch() ) {
echo 'Additional Description ' . $row['additional_description'] . '<br />';
}

1 comment:

  1. Plugin Company
    Easily create any type of Magento contact form with simple drag & drop form builder, 100’s design options and custom e-mail notifications!
    Mason Munter
    1050 McVaney Road
    Asheville, NC 28801
    masonmunter@gmail.com
    828-257-8345

    ReplyDelete

Fatal error: Class Zend\Stdlib\Parameters contains 1 abstract method... magento 2

Fatal error: Class Zend\Stdlib\Parameters contains 1 abstract method and must therefore be declared abstract or implement the remaining met...