Richard Castera

Application Developer/Designer
  • Home
  • About
  • Projects

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);
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Don't forget to Subscribe and Follow!

Be sure to subscribe to the feed and follow me on Twitter for more insights and resources!

5 Responses to “Magento – How to run a SQL query against the database”

  1. Comment #1 - Permalink
    Magento Designer
    01/07/2010

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

  2. Comment #2 - Permalink
    Ajax and PHP Developer
    03/17/2010

    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']);
    }

  3. Comment #3 - Permalink
    코베이
    03/20/2010

    here is my php test code…

    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….

  4. Comment #4 - Permalink
    코베이
    03/20/2010

    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’);

  5. Comment #5 - Permalink
    Jay
    04/19/2010

    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!

It's Your turn

Click here to cancel reply.
You

CAPTCHA Image
CAPTCHA Audio
Refresh Image



About The Author

Richard is a Web Developer currently working for SankyNet. He is available for freelance work. Visit Shifting Ideas for inquiries. Thanks!
  • Premium Email Templates

    Social Profiles

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google
  • GitHub

    Last Tweet

  • Beware the heat-seeking Nerf machine-gun coming to a cubicle near you http://om.ly/svHF #fb
  • Follow me
  • Archives

    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • October 2009
    • September 2009
    • August 2009
    • July 2009
    • June 2009
    • May 2009
    • September 2008
    • August 2008
    • July 2008
    • June 2008
  • Categories

    • AJAX
    • Apache
    • Books
    • Drupal
    • Ecommerce
    • Flash
    • Google
    • Javascript
    • jQuery
    • Magento
    • Marketing
    • Mootools
    • My Thoughts
    • MySQL
    • News
    • Photoshop
    • PHP
    • Prototype
    • SEO
    • Web Resources
    • Wordpress
  • My Favorites

    • 37 Signals
    • Andrew Warner
    • Chris Coyier
    • Chris Shiflett
    • Collis Ta'eed
    • Development Seed
    • Drupal
    • HubSpot
    • James Padolsey
    • Joel On Software
    • John Resig
    • jQuery
    • Kevin Rose
    • Life Hacker
    • Magento
    • Mashable
    • Matt Cutts
    • Matt Ryan
    • MySQL
    • NetTuts
    • Noupe
    • Photoshop Tutorials
    • PHP
    • Smashing Magazine
    • Tech Crunch
    • WoorkUp
    • Wordpress

2010 © Copyright. Richard Castera - All Rights Reserved.

Top