Database connection in magento
<?php
// Get the resource model
$resource = Mage::getSingleton('core/resource');
// Retrieve the read connection
$readConnection = $resource->getConnection('core_read');
// Retrieve the write connection
$writeConnection = $resource->getConnection('core_write');
//get table name
$tableName = $resource->getTableName('catalog_product_entity');
// or get table name from an entity name
$tableName = $resource->getTableName('catalog/product');
read select query in magento (read data from select query)
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
// Execute the query and store the results in $results
$results = $readConnection->fetchAll($query);
// Print out the results
print_r($results);
read only one column from select query in magento (read data from select query)
$name= $readConnection->fetchCol('SELECT name FROM ' . $tableName . ');
print_r($name);
read only one cell or fetchone item attribute value from product table
$productId = 101;
$query = 'SELECT name FROM ' . $tableName . ' WHERE entity_id = '. $productId . ' LIMIT 1';
$name = $readConnection->fetchOne($query);
echo $name;
update query in magento
$newSku = 'new-sku';
$updateQuery = "UPDATE {$tableName} SET sku = '{$sku}' WHERE entity_id = ".$productId;
$writeConnection->query($updateQuery );
?>
<?php
// Get the resource model
$resource = Mage::getSingleton('core/resource');
// Retrieve the read connection
$readConnection = $resource->getConnection('core_read');
// Retrieve the write connection
$writeConnection = $resource->getConnection('core_write');
//get table name
$tableName = $resource->getTableName('catalog_product_entity');
// or get table name from an entity name
$tableName = $resource->getTableName('catalog/product');
read select query in magento (read data from select query)
$query = 'SELECT * FROM ' . $resource->getTableName('catalog/product');
// Execute the query and store the results in $results
$results = $readConnection->fetchAll($query);
// Print out the results
print_r($results);
read only one column from select query in magento (read data from select query)
$name= $readConnection->fetchCol('SELECT name FROM ' . $tableName . ');
print_r($name);
read only one cell or fetchone item attribute value from product table
$productId = 101;
$query = 'SELECT name FROM ' . $tableName . ' WHERE entity_id = '. $productId . ' LIMIT 1';
$name = $readConnection->fetchOne($query);
echo $name;
update query in magento
$newSku = 'new-sku';
$updateQuery = "UPDATE {$tableName} SET sku = '{$sku}' WHERE entity_id = ".$productId;
$writeConnection->query($updateQuery );
?>
No comments :
Post a Comment