rss
twitter
    Find out what I'm doing, Follow Me :)

Wednesday, December 23, 2009

Pin It

Widgets

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(!$row) {
    return FALSE;
}

print_r($row);

Source : http://www.richardcastera.com/

1 comment:

  1. $conn = Mage::getSingleton('core/resource')->getConnection('core_read');

    // Prints a list of all website names
    $results = $conn->fetchAll("SELECT * FROM core_website;");
    foreach($results as $row) {
    echo $row['name'] . "\n";
    }

    ReplyDelete