Last active
May 26, 2016 02:49
-
-
Save nahidacm/5253766 to your computer and use it in GitHub Desktop.
Direct SQL Queries In Magento
http://nahid.me/magento/direct-sql-queries-in-magento/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Table names and table prefixes | |
$tableName = Mage::getSingleton('core/resource') | |
getTableName('catalog_product_entity'); | |
// if prefix was 'mage_' then the below statement | |
// would print out mage_catalog_product_entity | |
echo $tableName; | |
//Accessing the database connection resource | |
$read = Mage::getSingleton('core/resource')->getConnection('core_read'); | |
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); | |
//For a list of functions available, copy the following code into a Magento template. | |
$read = Mage::getSingleton('core/resource')->getConnection('core_read'); | |
echo '<pre>'; | |
print_r(get_class_methods($read)); | |
echo '</pre>'; | |
exit; | |
//Reading data from the database | |
$read = Mage::getSingleton('core/resource')->getConnection('core_read'); | |
$query = 'SELECT * FROM '. Mage::getSingleton('core/resource')->getTableName('catalog_product_entity'); | |
$results = $read->fetchAll($query); | |
print_r($results); | |
//Writing information to the database | |
$write = Mage::getSingleton('core/resource')->getConnection('core_write'); | |
// Add your own query below | |
// I didn't add one as I didn't want you to run the code | |
// and me break your database! | |
$query = 'add your query here'; | |
$write->query($query); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment