Magento has its own database logic and functions to execute the queries. We don't need to hardcode the query string as we can call the magento model functions.
However, we can also execute custom query whenever needed. The following script shows how we can achieve it,
From the magento controller:
protected $db;
$this->db = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "SELECT * FROM " . $this->db->getTableName('tablename') ;
$results = $this->db->fetchAll($query);
From anywhere in site,
/ * Get the resource model */
$resource = Mage::getSingleton(‘core/resource’);
/** Retrieve the read connection */
$readConnection = $resource->getConnection(‘core_read’);
$tableName = $resource->getTableName(‘catalog_product_entity’);
$query = "SELECT * FROM " . $tableName ;
However, we can also execute custom query whenever needed. The following script shows how we can achieve it,
From the magento controller:
protected $db;
$this->db = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "SELECT * FROM " . $this->db->getTableName('tablename') ;
$results = $this->db->fetchAll($query);
From anywhere in site,
/ * Get the resource model */
$resource = Mage::getSingleton(‘core/resource’);
/** Retrieve the read connection */
$readConnection = $resource->getConnection(‘core_read’);
$tableName = $resource->getTableName(‘catalog_product_entity’);
$query = "SELECT * FROM " . $tableName ;
Comments
Post a Comment