Write MySQL Database Query in Magento

If you need to execute a MySQL query anywhere in Magento (in the .php code files or .phtml template files), the following steps are required –

$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$conn->query("insert into table name (fields) values (values) ");

7 comments

  1. You can write the following to fetch:

    $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
    $result = $connection->query("select * from table");
    while ($row = $result->fetch() ) {
    $ids[]=$row['id'];
    }

  2. $con = Mage::getSingleton(‘core/resource’)->getConnection(‘core_write’);
    $result = $con->query(“select * from table”);
    while ($row = $result->fetch() )
    {
    $record[]=$row[‘field name’];
    }

Leave a comment

Your email address will not be published. Required fields are marked *