如果是在外部引用magento的sql語句苛聘,首先要先加載magento中的核心文件mage.php
require_once '../../app/Mage.php';
Mage::app('default');
1.對magento中的數(shù)據(jù)進(jìn)行select:Set the connection for Read the query :
<?php $connectionRead = Mage::getSingleton('core/resource')->getConnection('core_read'); ?>
$connectionRead object to execute the fetch query:
<?php $select = $connectionRead->select()
->from('tablename', array('*'))
->where('id=?',1)
->group('name');
$row =$connectionRead->fetchRow($select); //return rows
$result = $connectionRead->fetchAll($select); // This query will return a multidimensional Array.This work as a mysql_fetch_array() ?>
2.對magento中數(shù)據(jù)進(jìn)行插入:
Set the connection for Write query :
<?php $connectionWrite = Mage::getSingleton('core/resource')->getConnection('core_write'); ?>
$connectionWrite object will be used to execute insert query.
<?php $connectionWrite->beginTransaction();
$data = array();
$data['firstname']= 'abc';
$data['lastname']='cba';
$data['email']='abc@gmail.com';
$connectionWrite->insert('tablename', $data);
$connectionWrite->commit(); ?>
3.對magento中數(shù)據(jù)進(jìn)行更新:
$connectionWrite object will be used to execute update query.
<?php $connectionWrite->beginTransaction();
$data = array();
$data['firstname'] = 'xyz';
$where = $connectionWrite->quoteInto('tableId =?', '5');
$connectionWrite->update('tablename', $data, $where);
$connectionWrite->commit(); ?>
4.對magento中數(shù)據(jù)進(jìn)行刪除:
$connectionWrite object will be used to execute delete query.
<?php $connectionWrite->beginTransaction();
$condition = array($connectionWrite->quoteInto('tableId=?', '5'));
$connectionWrite->delete('tablename', $condition); ?>
注:在對個sql語句中多個條件時可以使用數(shù)組:比如查詢語句中多個where條件:
$write = Mage::getSingleton("core/resource")->getConnection('core_write');
$table = Mage::getSingleton('core/resource')->getTableName('sales_flat_quote_address');
$write->beginTransaction();
$where = $write->quoteInto(array('quote_id =?'=>$quoteId,'address_type =?'=>'shipping'));
$write->update($table,array('shipping_method'=>'tang_mycarrier'),$where);
$write->commit();