Interacting with a Magento database directly

15 Jun 2017 in Magento

Sometimes you may need to interact directly with the database of a Magento installation without using any models.

For this to work you may get the core sql adapter with:

$db = Mage::getSingleton("core/resource")
    ->getConnection("core_write"); // or "core_read" for reading

and use it like this:

$query = "INSERT INTO table(fields) VALUES (:values)";
$binds = ['values' => 'foo'];
$db->query($query, $binds);