Add code

Saturday 23 January 2016

How to run Custom Mysql Query in Magento?

 Magento uses Zend framework in very standard manner . It uses singleton methodology to access database.Like many other standard frameworks Magento also uses the concept of ORM (Object Relation Model) approach. To access the database Magento uses resource model where each magento model is divided into two categories
1. Active Record type
2. EAV (Entity Attribute Value) Model
With this resource model we can manipulate the data from our database.
But still if we want to run our own custom mysql query Magento provides a way to do this also. Here are the way to do


Because we need to read the data then we will get connection for core_read.
Suppose you need to insert some data into database then you need to take core_write.

$db = Mage::getSingleton('core/resource')->getConnection('core_read');
//Write your mysql query here
$result = $db->query("SELECT * FROM customerhistory WHERE customer_id = ".$customer->getId()." AND status <> 0 ORDER BY created_time DESC LIMIT 0,100");
//After running check whether data is available or not
if(!$result) {
echo 'No data found';
}
else
{
//Here we are fetching the data
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo $row["content"];
}
}

No comments:

Post a Comment

Magento : 404 error is showing admin page

Hello, Sometimes we may get the error on admin page once done with the Magento installation. In that scenario, we have to do the following: ...