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) ");
This entry was posted in Magento, PHP and tagged , . Bookmark the permalink.

8 Responses to Write MySQL Database Query in Magento

  1. Mithun says:

    Can you please tell me what to use instead of mysql_fetch_object or mysql_fetch_assoc in magento.

  2. BulbAmerica.com says:

    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() ) {

  3. kaushik says:

    Good Block…

  4. kaushik says:

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

  5. techfellas says:


    I can i get some particular data table in Quickbooks ?

    any idea ?

  6. techfellas says:

    sorry for the wrong sentence in my last comment!!!!!

    Actually my question is how i can get particular datatable in Quickbooks

    any idea ????

  7. Magento says:

    Hello codeboss,

    Is there any Magento extension that let’s you run a custom query in the admin panel ?

    I’ve been googling it but didn’t found any.

Leave a Reply

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