Tuesday, September 13

How to Read and Write to the Database in Magento

Here is the script to read and write to the database in magento

READ QUERIES

// Prepare the SQL
$sql = "SELECT * FROM mytable";
// Execute the SQL and return the results in to $getData
$getData = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchAll($sql);
// Parse the results
for($i=0; $i < count($getData); $i++){
    /* your code */
}
WRITE/UPDATE Queries
// $write is an instance of Zend_Db_Adapter_Abstract
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// Prepare the SQL statement
$sql  = "insert into tablename values (?, ?, ?)";
// Execute the SQL
$write->query($sql);
 
while ($row = $write->fetch() ) {
  /* your code */
}

Hope it Helps.. Thnaks....

No comments:

Post a Comment