Magento - How to run a SQL query against the database

In order to run a SQL query against the Magento database, you first need a resource model then a database connection.

$db = Mage::getResourceSingleton('core/resource')->getConnection('core_write');
$result = $db->query('SELECT 'entity_id' FROM 'catalog_product_entity');

if(!$result) {
    return FALSE;
}

$rows = $result->fetch(PDO::FETCH_ASSOC);

if(!$rows) {
    return FALSE;
}

print_r($rows);

7 comments for Magento - How to run a SQL query against the database

Magento Designer's picture

Thank you for sharing SQL...

Thank you for sharing SQL query against the Magento database. Nice post. keep writing.

Ajax and PHP Developer's picture

Just so everyone else knows....

Just so everyone else knows. To get rows of data do a while loop or similar.


$db = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$result = $db->query("select `id`,`name` from `dealerdoc_categories`");
if(!$result) {
return FALSE;
}
$categories=array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$categories[] = array('value'=>$row['id'],'label'=>$row['name']);
}

코베이's picture

here is my php test...

here is my php test code...


<?php
getConnection('core_write');
?>

this is error message...
Fatal error: Call to a member function getResourceModelInstance() on a non-object in /var/www/magento.homeunix.com/app/Mage.php on line 460

help me....please....

코베이's picture

reposting... my...

reposting...

my code..is...........


error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
require_once "app/Mage.php";
$db = Mage::getResourceSingleton('core/resource')->getConnection('core_write');

Jay's picture

Fixed a couple problems in...

Fixed a couple problems in the above code:
1. I added a limit of 10 to the statement so it doesn't crash your browser if you have a lot of products
2. Added "fetchAll" to the code so it fetches all the available rows (max 10 becuase of the above mentioned limit)
3. Added support for prefixes if your tables have a predefined prefix.
4. Changed to Mage::getSingleton('core/resource'). The above code is wrong.
5. Also, we should be using core_read here instead of core_write since we're only reading data, but we'll keep using core_write since it works for both.


$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$table_prefix = Mage::getConfig()->getTablePrefix();
$result = $db->query("SELECT entity_id FROM {$table_prefix}catalog_product_entity LIMIT 10");

if(!$result) {
return FALSE;
}

$rows = $result->fetchAll(PDO::FETCH_ASSOC);

if(!$rows) {
return FALSE;
}

print_r($rows);

Check out the Sweet Tooth reward points extension at http://www.getsweettooth.com which using this code somewhere in the source.

Cheers!

Jerome Dennis D's picture

Thanks !!! It has really...

Thanks !!!

It has really helped me.

@Jay Thanks for your corrections

fede's picture

Nice ! works for me in...

Nice ! works for me in app/design/frontend/default/default/template/page/2-colunms-left.phtml

saludos desde argentina

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.